-
Notifications
You must be signed in to change notification settings - Fork 7
/
pipe.gd
180 lines (160 loc) · 4.42 KB
/
pipe.gd
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
func reduce(fun : Callable, iterable : IterableBase, initial = GDRx.util.NOT_SET):
var it = iterable.iter()
var next_ = it.next()
var value_ = initial
while not next_ is ItEnd:
value_ = fun.call(value_, next_)
next_ = it.next()
return value_
func compose1(__op1 : Callable) -> Callable:
return compose(GDRx.util.Iter([__op1]))
func compose2(__op1 : Callable, __op2 : Callable) -> Callable:
return compose(GDRx.util.Iter([__op1, __op2]))
func compose3(
__op1 : Callable,
__op2 : Callable,
__op3 : Callable
) -> Callable:
return compose(GDRx.util.Iter([__op1, __op2, __op3]))
func compose4(
__op1 : Callable,
__op2 : Callable,
__op3 : Callable,
__op4 : Callable
) -> Callable:
return compose(GDRx.util.Iter([__op1, __op2, __op3, __op4]))
func compose5(
__op1 : Callable,
__op2 : Callable,
__op3 : Callable,
__op4 : Callable,
__op5 : Callable
) -> Callable:
return compose(GDRx.util.Iter([__op1, __op2, __op3, __op4, __op5]))
func compose6(
__op1 : Callable,
__op2 : Callable,
__op3 : Callable,
__op4 : Callable,
__op5 : Callable,
__op6 : Callable
) -> Callable:
return compose(GDRx.util.Iter([__op1, __op2, __op3, __op4, __op5, __op6]))
func composea(operators : Array[Callable]) -> Callable:
return compose(GDRx.util.Iter(operators))
## Compose multiple operators left to right.
## Composes zero or more operators into a functional composition. The
## operators are composed to left to right. A composition of zero
## operators gives back the source.
## [br]
## [b]Examples:[/b]
## [codeblock]
## pipe0().call(source) == source
## pipe1(f).call(source) == f.call(source)
## pipe2(f, g).call(source) == g.call(f.call(source))
## pipe3(f, g, h).call(source) == h.call(g.call(f.call(source)))
## ...
## [/codeblock]
## [br][br]
## [b]Returns:[/b]
## [br]
## The composed observable.
func compose(operators : IterableBase) -> Callable:
var _compose = func(source):
return reduce(func(obs, op): return op.call(obs), operators, source)
return _compose
func pipe0(_value) -> Variant:
return pipe(_value, GDRx.util.Iter([]))
func pipe1(__value : Variant, __fn1 : Callable) -> Variant:
return pipe(__value, GDRx.util.Iter([__fn1]))
func pipe2(
__value : Variant,
__fn1 : Callable,
__fn2 : Callable
) -> Variant:
return pipe(__value, GDRx.util.Iter([__fn1, __fn2]))
func pipe3(
__value : Variant,
__fn1 : Callable,
__fn2 : Callable,
__fn3 : Callable
) -> Variant:
return pipe(__value, GDRx.util.Iter([__fn1, __fn2, __fn3]))
func pipe4(
__value : Variant,
__fn1 : Callable,
__fn2 : Callable,
__fn3 : Callable,
__fn4 : Callable
) -> Variant:
return pipe(__value, GDRx.util.Iter([__fn1, __fn2, __fn3, __fn4]))
func pipe5(
__value : Variant,
__fn1 : Callable,
__fn2 : Callable,
__fn3 : Callable,
__fn4 : Callable,
__fn5 : Callable
) -> Variant:
return pipe(__value, GDRx.util.Iter([__fn1, __fn2, __fn3, __fn4, __fn5]))
func pipe6(
__value : Variant,
__fn1 : Callable,
__fn2 : Callable,
__fn3 : Callable,
__fn4 : Callable,
__fn5 : Callable,
__fn6 : Callable
) -> Variant:
return pipe(__value, GDRx.util.Iter([__fn1, __fn2, __fn3, __fn4, __fn5, __fn6]))
func pipe7(
__value : Variant,
__fn1 : Callable,
__fn2 : Callable,
__fn3 : Callable,
__fn4 : Callable,
__fn5 : Callable,
__fn6 : Callable,
__fn7 : Callable
) -> Variant:
return pipe(__value, GDRx.util.Iter([__fn1, __fn2, __fn3, __fn4, __fn5, __fn6, __fn7]))
func pipe8(
__value : Variant,
__fn1 : Callable,
__fn2 : Callable,
__fn3 : Callable,
__fn4 : Callable,
__fn5 : Callable,
__fn6 : Callable,
__fn7 : Callable,
__fn8 : Callable
) -> Variant:
return pipe(__value, GDRx.util.Iter([__fn1, __fn2, __fn3, __fn4, __fn5, __fn6, __fn7, __fn8]))
func pipe9(
__value : Variant,
__fn1 : Callable,
__fn2 : Callable,
__fn3 : Callable,
__fn4 : Callable,
__fn5 : Callable,
__fn6 : Callable,
__fn7 : Callable,
__fn8 : Callable,
__fn9 : Callable
) -> Variant:
return pipe(__value, GDRx.util.Iter([__fn1, __fn2, __fn3, __fn4, __fn5, __fn6, __fn7, __fn8, __fn9]))
func pipea(__value, arr : Array):
return pipe(__value, GDRx.util.Iter(arr))
## Functional pipe (`|>`)
## [br]
## Allows the use of function argument on the left side of the
## function.
## [br]
## [b]Example:[/b]
## [codeblock]
## pipe2(x, fn) == __fn.call(x) # Same as x |> fn
## pipe3(x, fn, gn) == gn.call(fn.call(x)) # Same as x |> fn |> gn
## ...
## [/codeblock]
func pipe(__value : Variant, fns : IterableBase) -> Variant:
return compose(fns).call(__value)