-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.js
89 lines (68 loc) · 2.04 KB
/
list.js
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
var promiseT, a, a2, PA, pa, pa2, when;
// A promise lib we'll use to make promises
when = require('when');
// promiseT Monad transformer that adds promised
// asynchrony to any Functor, Applicative, Monad, etc.
promiseT = require('../promiseT');
// Javascript Array isn't a Monad, but Array.prototype.map
// is a Functor, so we can still use it.
PA = promiseT(Array);
// Plain old Array
a = [1,2,3];
// log all the values
a.map(log);
// Array whose elements are promises
pa = a.map(when);
// try to log all the values
// Hmmm, doesn't do what we want
// logs: [object],[object],[object]
pa.map(log);
// Let's lift it and make it understand promises
// Now pa is a list that knows how to deal with the fact
// that its elements are promises
pa = PA.lift(pa);
// Now log it. Yay, it logs values!
// logs: 1, 2, 3
pa.map(log);
// Let's see what else works
// We know map works, but let's test it further
a.map(addOne).map(log);
// We can do the same with lists of promises.
pa.map(addOne).map(log);
// We know we can concat Arrays
a2 = [4,5,6];
a.concat(a2).map(log);
// Hey look, we can concat lists of promises too, easy.
pa2 = PA.lift(a2.map(when));
pa.concat(pa2).map(log);
// Arrays are foldable, and lists of promises are now too
// Folding a list of values yields a value
log(a.reduceRight(add, 0));
// Folding a list of promises yields a promise, fun!
pa.reduceRight(add, when(0)).then(log);
// Similarly, from the left
log(a.reduce(add, 0));
pa.reduce(add, when(0)).then(log);
// And we can put it all together
log(a.concat(a2).map(addOne).reduce(add, 0));
pa.concat(pa2).map(addOne).reduce(add, when(0)).then(log);
// Is it really async? Let's see
pa = PA.lift([delay(3000, 1), delay(2000, 2), delay(1000, 3)]);
pa.map(log);
pa.reduce(add, when(0)).then(log);
log('If you see this before any of the PA results, then yep, it\'s all async!');
function log(x) {
console.log(x);
return x;
}
function addOne(x) {
return x+1;
}
function add(x, y) {
return x + y;
}
function delay(ms, x) {
return when.promise(function(r) {
setTimeout(r.bind(null, x), ms);
});
}