/
funcs.go
99 lines (85 loc) · 2.1 KB
/
funcs.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package iter
// Any represents any type.
type Any interface{}
type (
// UnaryPredicate checks if a value satisfy condition.
UnaryPredicate func(Any) bool
// EqComparer checks if first value equals to the second value.
EqComparer func(Any, Any) bool
// LessComparer checks if first value is less than the second value.
LessComparer func(Any, Any) bool
// ThreeWayComparer compares 2 values, returns 1 if first>second, 0 if
// first=second, -1 if first<second.
ThreeWayComparer func(Any, Any) int
// IteratorFunction apply some actions to a value.
IteratorFunction func(Any)
// UnaryOperation transforms a value to another.
UnaryOperation func(Any) Any
// BinaryOperation transforms 2 values to 1 value.
BinaryOperation func(Any, Any) Any
// Generator creates a value on each call.
Generator func() Any
)
func _eq(x, y Any) bool {
type ieq interface{ Eq(Any) bool }
if e, ok := x.(ieq); ok {
return e.Eq(y)
}
return x == y
}
func _ne(x, y Any) bool {
return !_eq(x, y)
}
func _less(x, y Any) bool {
type iless interface{ Less(Any) bool }
if c, ok := x.(iless); ok {
return c.Less(y)
}
return _cmp(x, y) < 0
}
func _cmp(x, y Any) int {
type icmp interface{ Cmp(Any) int }
if t, ok := x.(icmp); ok {
return t.Cmp(y)
}
return reflectCmp(x, y)
}
func _inc(x Any) Any {
type iinc interface{ Inc() Any }
if i, ok := x.(iinc); ok {
return i.Inc()
}
return reflectInc(x)
}
func _add(x, y Any) Any {
type iadd interface{ Add(Any) Any }
if a, ok := x.(iadd); ok {
return a.Add(y)
}
return reflectAdd(x, y)
}
func _sub(x, y Any) Any {
type isub interface{ Sub(Any) Any }
if s, ok := x.(isub); ok {
return s.Sub(y)
}
return reflectSub(x, y)
}
func _mul(x, y Any) Any {
type imul interface{ Mul(Any) Any }
if m, ok := x.(imul); ok {
return m.Mul(y)
}
return reflectMul(x, y)
}
// Returns a Predicate that returns true if the value equals v.
func _eq1(v Any) UnaryPredicate {
return func(x Any) bool {
return _eq(v, x)
}
}
func _not1(p UnaryPredicate) UnaryPredicate {
return func(x Any) bool { return !p(x) }
}
func _true1(Any) bool { return true }
func _noop(x Any) Any { return x }