🛠 Added TernCall util

This commit is contained in:
Dan Jones 2023-10-29 11:58:00 -05:00
commit 0aa0ac284e
2 changed files with 32 additions and 0 deletions

View file

@ -36,3 +36,10 @@ func Tern[V any](choice bool, one, two V) V {
}
return two
}
func TernCall[V any](choice bool, one, two func() V) V {
if choice {
return one()
}
return two()
}

25
utils/utils_tern_test.go Normal file
View file

@ -0,0 +1,25 @@
package utils
import (
"testing"
)
func TestTern(t *testing.T) {
out := Tern(true, 5, 2)
if out != 5 {
t.Fatalf(`Tern(%v, %d, %d) = %v, want %d`, true, 5, 2, out, 5)
}
}
func TestTernCall(t *testing.T) {
five := func() int {
return 5
}
two := func() int {
return 2
}
out := TernCall(true, five, two)
if out != 5 {
t.Fatalf(`TernCall(%v, func () { return 5}, func () {return 2}) = %v, want %d`, true, out, 5)
}
}