-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfutureExercise.js
141 lines (98 loc) · 3.4 KB
/
futureExercise.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var _ = require('ramda');
var P = require('pointfree-fantasy');
var Future = require('data.future');
var map = _.map;
var compose = P.compose;
var Maybe = require('pointfree-fantasy/instances/maybe');
var Identity = P.I;
var IO = require('./IO');
var Bacon = require('bacon');
IO.extendFn();
// Exercise 1
// ==========
// Use getPost(id) to return a Future of the title of the post ({id: i, title: 'Love them futures'})
console.log("--------Start exercise 1--------");
var ex1 = _.compose(map(_.prop('title')), getPost);
ex1(3).fork(console.error, function(title){
assertEqual('Love them futures', title);
console.log("exercise 1..ok!");
});
// Exercise 2
// ==========
// Use ex1 to extend the computation and render the title in a div
console.log("--------Start exercise 2--------");
var render = function(x){ return "<div>"+x+"</div>"; };
var ex2 = _.compose(map(render), ex1);
ex2(3).fork(log, function(html){
assertEqual('<div>Love them futures</div>', html);
console.log("exercise 2...ok!");
});
// Exercise 3
// ==========
// In JSBin, click the "Output" tab to see a div. Click this div to run the test.
// Turn the clicks into a stream of the div's innerHTML
//console.log("--------Start exercise 3--------")
//
//var clicks = Bacon.fromEventTarget(document.querySelector("#box"), "click")
//
////Todo: turn clicks into a stream of the e.target.innerHTML
//var htmlClicks = clicks;
//
//htmlClicks.onValue(function(html){
// assertEqual('<span>CLICK ME</span>', trim(html))
// console.log("exercise 3...ok!")
//})
// Exercise 4
// ==========
// Keep the Output tab open. Type into the input to run the test.
// Transform the keydowns into a stream of the input's value
// Then use pureLog() to log it to the console
//console.log("--------Start exercise 4--------")
//
//var pureLog = function(x){ console.log(x); return x; }.toIO();
//var search_input = document.querySelector("#search")
//var keydowns = Bacon.fromEventTarget(search_input, "keydown")
//
////Todo: turn keydowns into a stream of the logged input's value
//var logs = keydowns;
//
//logs.onValue(function(io){
// assertEqual(search_input.value, runIO(io))
// console.log("exercise 4...ok!")
//})
// Exercise 5*
// ==========
// Use only safeGet() to safely return the street name
console.log("--------Start exercise 5--------");
var safeGet = _.curry(function(x,o){ return Maybe(o[x]) });
var user = {id: 2, name: "Albert", address: { street: {number: 22, name: 'Walnut St'} } };
var ex5 = _.compose(safeGet('name'), safeGet('street'), safeGet('address'));
assertDeepEqual(Maybe(Maybe(Maybe('Walnut St'))), ex5(user));
console.log("exercise 5...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(inspectIt(x) !== inspectIt(y)) throw("expected "+inspectIt(x)+" to equal "+inspectIt(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', ''); }