-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonadExercises.js
119 lines (72 loc) · 2.64 KB
/
monadExercises.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
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
var _ = require('ramda');
var P = require('pointfree-fantasy');
var Future = require('data.future');
var map = _.map;
var compose = P.compose;
var chain = P.chain;
var Maybe = require('pointfree-fantasy/instances/maybe');
var Identity = P.I;
var IO = require('./IO');
var Bacon = require('bacon');
IO.extendFn();
// Exercise 1
// ==========
// Use safeGet and mjoin or chain to safetly get the street name
console.log("--------Start exercise 1--------");
var safeGet = _.curry(function(x,o){ return Maybe(o[x]) });
var user = {id: 2, name: "Albert", address: { street: {number: 22, name: 'Walnut St'} } };
// mjoin
//var ex1 = compose(P.mjoin, map(safeGet('name')), P.mjoin, map(safeGet('street')), safeGet('address'));
// chain (because mjoin, map === chain
var ex1 = compose(chain(safeGet('name')), chain(safeGet('street')), safeGet('address'));
assertDeepEqual(Maybe('Walnut St'), ex1(user));
console.log("exercise 1...ok!");
// Exercise 2
// ==========
// Use monads to get the href, then purely log it.
console.log("--------Start exercise 2--------");
var getHref = function(){ return 'http://run.jsbin.io/runner' }.toIO();
var pureLog = function(x){ console.log(x); return x; }.toIO();
// mjoin version
//var ex2 = compose(P.mjoin, map(pureLog), getHref);
// chain version
var ex2 = compose(chain(pureLog), getHref);
assertEqual("http://run.jsbin.io/runner", IO.runIO(ex2(null)));
console.log("exercise 2...ok!");
// Exercise 3
// ==========
// Use monads to first get the Post with getPost(), then pass it's id in to getComments().
console.log("--------Start exercise 3--------");
//var ex3 = compose(P.mjoin, map(getComments), map(safeGet('id')), getPost);
var ex3 = compose(chain(getComments), map(safeGet('id')), getPost);
ex3(13).fork(log, function(res){
assertEqual(2, res.length);
console.log("exercise 3...ok!")
});
// TEST HELPERS
// =====================
function inspectIt(x){
return (x.inspect && x.inspect()) || (x.toString && x.toString()) || x.valueOf(); //hacky for teachy.
}
function assertEqual(x,y){
if(x !== y){ throw("expected "+x+" to equal "+y); }
}
function assertDeepEqual(x,y){
if(x.val !== y.val) throw("expected "+inspectIt(JSON.stringify(x))+" to equal "+inspectIt(JSON.stringify(y)));
}
function log(x){ console.log(x); return x; }
function getPost(i) {
return new Future(function(rej, res) {
setTimeout(function(){
res({id: i, title: 'Love them futures'})
}, 300)
})
}
function getComments(i) {
return new Future(function(rej, res) {
setTimeout(function(){
res(["This class should be illegal", "Monads are like space burritos"])
}, 300)
})
}
function trim(x){ return x.replace('/\S{0,}/g', ''); }