This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinterpreter-tweet.js
98 lines (83 loc) · 2.61 KB
/
interpreter-tweet.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
'use strict';
const daggy = require('daggy');
const {identity} = require('fantasy-combinators');
const {Free} = require('./../fantasy-frees');
const {singleton} = require('fantasy-helpers');
const Cofree = require('fantasy-cofrees');
const Identity = require('fantasy-identities');
const Option = require('fantasy-options');
const Tweet = daggy.tagged('id', 'str');
const User = daggy.tagged('id', 'name', 'photo');
const Service = daggy.taggedSum({
GetTweets : ['id'],
GetUserName : ['id'],
GetUserPhoto : ['id']
});
const Request = daggy.taggedSum({
Fetch : ['x'],
Pure : ['x']
});
function pure(x) {
return Free.liftFC(Request.Pure(x));
}
function fetch(s) {
return Free.liftFC(Request.Fetch(s));
}
function arrayNel(x) {
function go(y, z) {
return y.length < 1 ? z.x : go(y.slice(0, -1), Option.Some(Cofree(y.slice(-1)[0], z)));
}
return go(x, Option.None);
}
function nelArray(xs) {
return [xs.a].concat(xs.f.cata({
Some: (x) => nelArray(x),
None: () => []
}));
}
Cofree.prototype.sequence = function(p) {
return this.traverse(identity, p);
};
const interpreters = {
pure : (req) => {
const res = req.cata({
Pure: identity,
Fetch: (s) => {
return s.cata({
GetTweets: (id) => {
return arrayNel([Tweet(1, 'Hello'), Tweet(2, 'World'), Tweet(3, '!')]);
},
GetUserName: (id) => {
return id === 1 ? 'Tim'
: id === 2 ? 'Bob'
: 'Anonymous';
},
GetUserPhoto: (id) => {
return id === 1 ? ':-)'
: id === 2 ? ':-D'
: ':-|';
}
});
}
});
return Identity.of(res);
}
};
function getUser(id) {
return fetch(Service.GetUserName(id)).chain((name) => {
return fetch(Service.GetUserPhoto(id)).map((photo) => {
return User(id, name, photo);
});
});
}
const id = 1;
const script = fetch(Service.GetTweets(id)).chain((tweets) => {
return tweets.map((tweet) => {
return getUser(tweet.id).map((user) => {
return singleton(tweet.str, user);
});
}).sequence(Free);
});
console.log('---------------------------------------------------------');
console.log(nelArray(Free.runFC(script, interpreters.pure, Identity).x));
console.log('---------------------------------------------------------');