From 0aa0ac284e2fe0e49aaa83209a762a2a48d816c3 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 29 Oct 2023 11:58:00 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=20Added=20TernCall=20util?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/utils.go | 7 +++++++ utils/utils_tern_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 utils/utils_tern_test.go diff --git a/utils/utils.go b/utils/utils.go index 197b898..17d76d5 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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() +} diff --git a/utils/utils_tern_test.go b/utils/utils_tern_test.go new file mode 100644 index 0000000..debbddb --- /dev/null +++ b/utils/utils_tern_test.go @@ -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) + } +}