Last active
January 29, 2026 01:37
-
-
Save thanhminhmr/05593d24fd3ee533e5e1970ea82e3952 to your computer and use it in GitHub Desktop.
Revisions
-
thanhminhmr revised this gist
Mar 25, 2025 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,18 @@ package ternary func If[T any](condition bool, ifTrue Supplier[T], ifFalse Supplier[T]) T { if condition { return ifTrue.Get() } return ifFalse.Get() } func Func[T any](supplier func() T) Supplier[T] { return FuncSupplier[T](supplier) } func Value[T any](value T) Supplier[T] { return ValueSupplier[T]{Value: value} } type Supplier[T any] interface { @@ -26,9 +26,9 @@ func (s FuncSupplier[T]) Get() T { } type ValueSupplier[T any] struct { Value T } func (s ValueSupplier[T]) Get() T { return s.Value } -
thanhminhmr created this gist
Mar 25, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ # go-ternary Yes, I know—yet another attempt at bringing a ternary-like experience to Go. But hey, Go doesn’t have one, and I wasn’t around when the last million were written. ## Why? Because Go doesn't have a ternary operator, and according to the official FAQ, it likely never will. The reasoning? To prevent developers from writing "impenetrably complex expressions." But let's be real—poor coding practices exist in all forms. Instead of outright banning a useful construct, wouldn’t compiler warnings for overly complicated ternary expressions have been a more reasonable approach? Since that's not happening, here’s `go-ternary`—because sometimes, a one-liner is just nicer than an `if-else`. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ package ternary func If[T any](condition bool, a Supplier[T], b Supplier[T]) T { if condition { return a.Get() } return b.Get() } func Func[T any](supplier func() T) Supplier[T] { return FuncSupplier[T](supplier) } func Value[T any](value T) Supplier[T] { return ValueSupplier[T]{value: value} } type Supplier[T any] interface { Get() T } type FuncSupplier[T any] func() T func (s FuncSupplier[T]) Get() T { return s() } type ValueSupplier[T any] struct { value T } func (s ValueSupplier[T]) Get() T { return s.value } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ package ternary import "testing" func assertEquals[T comparable](t *testing.T, a T, b T) { if a != b { t.Errorf("assertEquals failed: %v != %v", a, b) } } func Test(t *testing.T) { assertEquals(t, If(true, Value("true"), Value("false")), "true") assertEquals(t, If(false, Value("true"), Value("false")), "false") assertEquals(t, If(true, Func(func() string { return "true" }), Func(func() string { t.Error("lazyEvaluate failed: this func should not be called") return "false" })), "true") assertEquals(t, If(false, Func(func() string { t.Error("lazyEvaluate failed: this func should not be called") return "true" }), Func(func() string { return "false" })), "false") }