🛠 Added TernCall util
This commit is contained in:
parent
0a55d514fc
commit
0aa0ac284e
2 changed files with 32 additions and 0 deletions
|
|
@ -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
25
utils/utils_tern_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue