-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathif.go
70 lines (59 loc) · 1.16 KB
/
if.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package fun
func IF[T any](predicate bool, ifTrue, ifFalse T) T {
if predicate {
return ifTrue
}
return ifFalse
}
type ifElse[T any] struct {
value func() T
predicate bool
}
func If[T any](predicate bool, value T) ifElse[T] { //nolint:revive // don't export type
return ifElse[T]{
predicate: predicate,
value: func() T { return value },
}
}
func IfF[T any](predicate bool, value func() T) ifElse[T] { //nolint:revive // don't export type
return ifElse[T]{
predicate: predicate,
value: value,
}
}
func (i ifElse[T]) ElseIf(predicate bool, value T) ifElse[T] {
if i.predicate {
return i
}
return ifElse[T]{
predicate: predicate,
value: func() T { return value },
}
}
func (i ifElse[T]) ElseIfF(predicate bool, value func() T) ifElse[T] {
if i.predicate {
return i
}
return ifElse[T]{
predicate: predicate,
value: value,
}
}
func (i ifElse[T]) Else(value T) T {
if i.predicate {
return i.value()
}
return value
}
func (i ifElse[T]) ElseF(value func() T) T {
if i.predicate {
return i.value()
}
return value()
}
func (i ifElse[T]) ElseDeref(value *T) T {
if i.predicate {
return i.value()
}
return *value
}