-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
334 lines (313 loc) · 10.7 KB
/
functions.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**********************************************************\
* *
* promise/functions.go *
* *
* some functions of promise for Go. *
* *
* LastModified: Sep 11, 2016 *
* Author: Ma Bingyao <[email protected]> *
* *
\**********************************************************/
package promise
import (
"errors"
"reflect"
"sync/atomic"
"time"
)
// Create creates a Promise object containing the result of asynchronously
// calling computation.
//
// If calling computation returns error, the returned Promise is rejected with
// the error.
//
// If calling computation returns a Promise object, completion of the created
// Promise will wait until the returned Promise completes, and will then
// complete with the same result.
//
// If calling computation returns a non-Promise value, the returned Promise is
// completed with that value.
func Create(computation Computation) Promise {
promise := New()
go call(promise, reflect.ValueOf(computation), reflect.Value{})
return promise
}
// Sync creates a Promise object containing the result of immediately calling
// computation.
//
// If calling computation returns error, the returned Promise is rejected with
// the error.
//
// If calling computation returns a Promise object, completion of the created
// Promise will wait until the returned Promise completes, and will then
// complete with the same result.
//
// If calling computation returns a non-Promise value, the returned Promise is
// completed with that value.
func Sync(computation Computation) Promise {
promise := New()
call(promise, reflect.ValueOf(computation), reflect.Value{})
return promise
}
// Delayed creates a Promise object with the given value after a delay.
//
// If the value is a Callable function, it will be executed after the given
// duration has passed, and the Promise object is completed with the result.
func Delayed(duration time.Duration, value interface{}) Promise {
promise := New()
go func() {
time.Sleep(duration)
computation := reflect.ValueOf(value)
if computation.Kind() == reflect.Func {
call(promise, computation, reflect.Value{})
} else {
promise.Resolve(value)
}
}()
return promise
}
// All function returns a promise that resolves when all of the promises in the
// iterable argument have resolved, or rejects with the reason of the first
// passed promise that rejects.
func All(iterable ...interface{}) Promise {
count := int64(len(iterable))
if count == 0 {
return Resolve(nil)
}
result := make([]interface{}, count)
promise := New()
for index, value := range iterable {
ToPromise(value).Then(
allHandler(promise, result, index, &count),
promise.Reject)
}
return promise
}
func allHandler(promise Promise, result []interface{}, index int, count *int64) func(value interface{}) {
return func(value interface{}) {
result[index] = value
if atomic.AddInt64(count, -1) == 0 {
promise.Resolve(result)
}
}
}
// Race function returns a promise that resolves or rejects as soon as one of
// the promises in the iterable resolves or rejects, with the value or reason
// from that promise.
func Race(iterable ...interface{}) Promise {
promise := New()
for _, value := range iterable {
ToPromise(value).Fill(promise)
}
return promise
}
// Any function is a competitive race that allows one winner.
//
// The returned promise will fulfill as soon as any one of the input promises
// fulfills, with the value of the fulfilled input promise.
//
// Or reject with a IllegalArgumentError if the input iterable is empty--i.e. it
// is impossible to have one winner.
//
// Or reject with an iterable of all the rejection reasons, if the input
// iterable is non-empty, and all input promises reject.
func Any(iterable ...interface{}) Promise {
count := int64(len(iterable))
if count == 0 {
return Reject(IllegalArgumentError("Any(): iterable must not be empty"))
}
promise := New()
for _, value := range iterable {
ToPromise(value).Then(promise.Resolve, anyHandler(promise, &count))
}
return promise
}
func anyHandler(promise Promise, count *int64) func() {
return func() {
if atomic.AddInt64(count, -1) == 0 {
promise.Reject(errors.New("Any(): all promises failed"))
}
}
}
// Each function executes a provided function once per element of iterable.
//
// The callback parameter is a function to execute for each element:
//
// func(value interface{})
//
// value: The current element being processed.
//
// If any of the promises in iterable is rejected, the callback will not be
// executed. the returned promise will be rejected with the rejection reason
// of the first promise that was rejected.
func Each(callback func(interface{}), iterable ...interface{}) Promise {
return All(iterable...).Then(func(a interface{}) {
if a != nil {
each(callback, a.([]interface{}))
}
})
}
func each(callback func(interface{}), iterable []interface{}) {
for _, value := range iterable {
callback(value)
}
}
// Every function tests whether all elements in the iterable pass the test
// implemented by the provided function.
//
// The callback parameter is a function to test for each element:
//
// func(value interface{}) bool
//
// value: The current element being processed.
//
// If any of the promises in iterable is rejected, the callback will not be
// executed. the returned promise will be rejected with the rejection reason
// of the first promise that was rejected.
//
// The returned promise will fulfill with false if the callback function returns
// false for any iterable element immediately. Otherwise, if callback returned
// a true value for all elements, Every will return promise fulfill with true.
//
// If iterable is empty, The returned promise will fulfill with true.
func Every(callback func(interface{}) bool, iterable ...interface{}) Promise {
return All(iterable...).Then(func(a interface{}) bool {
if a == nil {
return true
}
return every(callback, a.([]interface{}))
})
}
func every(callback func(interface{}) bool, iterable []interface{}) bool {
for _, value := range iterable {
if !callback(value) {
return false
}
}
return true
}
// Some function tests whether some element in the iterable passes the test
// implemented by the provided function.
//
// The callback parameter is a function to test for each element:
//
// func(value interface{}) bool
//
// value: The current element being processed.
//
// If any of the promises in iterable is rejected, the callback will not be
// executed. the returned promise will be rejected with the rejection reason
// of the first promise that was rejected.
//
// The returned promise will fulfill with true if the callback function returns
// true for any iterable element immediately. Otherwise, if callback returned a
// false value for all elements, Some will return promise fulfill with false.
//
// If iterable is empty, The returned promise will fulfill with false.
func Some(callback func(interface{}) bool, iterable ...interface{}) Promise {
return All(iterable...).Then(func(a interface{}) bool {
if a == nil {
return false
}
return some(callback, a.([]interface{}))
})
}
func some(callback func(interface{}) bool, iterable []interface{}) bool {
for _, value := range iterable {
if callback(value) {
return true
}
}
return false
}
// Filter function returns a promise fulfill with a slice that has all elements
// pass the test implemented by the provided function.
//
// The callback parameter is a function to test for each element:
//
// func(value interface{}) bool
//
// value: The current element being processed.
//
// If any of the promises in iterable is rejected, the callback will not be
// executed. the returned promise will be rejected with the rejection reason
// of the first promise that was rejected.
func Filter(callback func(interface{}) bool, iterable ...interface{}) Promise {
return All(iterable...).Then(func(a interface{}) []interface{} {
if a == nil {
return nil
}
return filter(callback, a.([]interface{}))
})
}
func filter(callback func(interface{}) bool, iterable []interface{}) []interface{} {
result := make([]interface{}, 0, len(iterable))
for _, value := range iterable {
if callback(value) {
result = append(result, value)
}
}
return result
}
// Map function returns a promise fulfill with a slice that has the results of
// calling a provided function on every element in iterable.
//
// The callback parameter produces an element of the new slice:
//
// func(value interface{}) interface{}
//
// value: The current element being processed.
//
// If any of the promises in iterable is rejected, the callback will not be
// executed. the returned promise will be rejected with the rejection reason
// of the first promise that was rejected.
func Map(callback func(interface{}) interface{}, iterable ...interface{}) Promise {
return All(iterable...).Then(func(a interface{}) []interface{} {
if a == nil {
return nil
}
iterable := a.([]interface{})
result := make([]interface{}, len(iterable))
for index, value := range iterable {
result[index] = callback(value)
}
return result
})
}
// Reduce function applies a function against an accumulator and each value of
// the iterable (from left-to-right) to reduce it to a single value.
//
// The callback parameter executes on each value in the iterable, taking three
// arguments:
//
// func(prev interface{}, value interface{}) interface{}
//
// prev: The value previously returned in the last invocation of the callback.
//
// value: The current element being processed.
//
// The first time the callback is called, prev will be equal to the first value
// in the iterable and value will be equal to the second.
//
// If any of the promises in iterable is rejected, the callback will not be
// executed. the returned promise will be rejected with the rejection reason
// of the first promise that was rejected.
//
// If iterable is empty, the returned promise will be rejected with a
// IllegalArgumentError.
func Reduce(callback func(interface{}, interface{}) interface{}, iterable ...interface{}) Promise {
return All(iterable...).Then(func(a interface{}) (interface{}, error) {
if a == nil {
return nil, IllegalArgumentError("Reduce(): iterable must not be empty")
}
return reduce(callback, a.([]interface{})), nil
})
}
func reduce(callback func(interface{}, interface{}) interface{}, iterable []interface{}) interface{} {
count := len(iterable)
result := iterable[0]
for index := 1; index < count; index++ {
result = callback(result, iterable[index])
}
return result
}