Documentation ¶
Overview ¶
cond gives condition control flow
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IfCond ¶
type IfCond[T, R any] struct { // contains filtered or unexported fields }
IfCond is a if condition control flow structure.
Example ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/syntax/cond" ) func main() { a := cond.If(func(t int) bool { return t == 0 }, func(t int) string { return "zero" }).ElseIf(func(i int) bool { return i == 1 }, func(i int) string { return "one" }).Else(func(i int) string { return "other" }) fmt.Println(a.Run(0)) fmt.Println(a.Run(1)) fmt.Println(a.Run(2)) }
Output: zero one other
type SwitchCond ¶
type SwitchCond[T comparable, R any] struct { // contains filtered or unexported fields }
SwitchCond is a switch condition control flow structure.
Example ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/syntax/cond" ) func main() { a := cond.Switch[int, string]().Case(0, func(i int) string { return "zero" }).Case(1, func(i int) string { return "one" }).Default(func(i int) string { return "other" }) fmt.Println(a.Run(0)) fmt.Println(a.Run(1)) fmt.Println(a.Run(2)) }
Output: zero one other
Example (Result) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/syntax/cond" "github.com/snowmerak/generics-for-go/v2/types/result" ) func main() { result := result.OK(100) resultCond := cond.Switch[bool, int]().Case(true, func(b bool) int { return result.Unwrap() }).Default(func(b bool) int { return -99 }) fmt.Println(resultCond.Run(result.IsOK())) }
Output: 100
func (*SwitchCond[T, R]) Case ¶
func (s *SwitchCond[T, R]) Case(value T, result func(T) R) *SwitchCond[T, R]
Case adds a new condition to the SwitchCond.
func (*SwitchCond[T, R]) Default ¶
func (s *SwitchCond[T, R]) Default(defaultElse func(T) R) *SwitchCond[T, R]
Default adds a default result function to the SwitchCond.
func (*SwitchCond[T, R]) Run ¶
func (s *SwitchCond[T, R]) Run(t T) (rs R)
Run runs the SwitchCond with given value.
Click to show internal directories.
Click to hide internal directories.