@@ -4,13 +4,16 @@ const { List } = require('immutable-ext')
4
4
5
5
const Task = require ( 'data.task' )
6
6
7
- // 'chain' does not exist on the array,
7
+ // 'chain' (aka 'flatMap') does not exist on the array,
8
8
// so we apply natural transformation into the List first
9
+ // array -> List(array) is a natural transformation, it changes structure but not content
9
10
const res = List ( [ 'hello' , 'world' ] )
10
11
. chain ( x => List ( x . split ( '' ) ) )
11
12
12
- console . log (
13
- `List(['hello', 'world']).chain( x => List(x.split('')) ) : ` ,
13
+ console . log ( `
14
+ List(['hello', 'world'])
15
+ .chain( x => List(x.split('')) ) :
16
+ ` ,
14
17
res
15
18
)
16
19
@@ -28,33 +31,36 @@ const larger = x =>
28
31
x * 2
29
32
30
33
31
- const app = xs =>
34
+ // 'first' is a natural transform, so it commutes with 'map'
35
+ // 'first' changes structure but not content
36
+ // 'map' is functor transform, it changes content but not structure
32
37
38
+ const app = xs =>
33
39
// map, then first
34
40
first (
35
41
largeNumbers ( xs )
36
- . map ( larger )
42
+ . map ( larger )
37
43
)
38
44
39
- console . log (
40
- ` app([2, 400, 5, 1000]) : `,
45
+ console . log ( `
46
+ app([2, 400, 5, 1000]) : ` ,
41
47
app ( [ 2 , 400 , 5 , 1000 ] )
42
48
)
43
49
44
50
const app1 = xs =>
45
-
46
51
// first, then map
47
- first (
48
- largeNumbers ( xs )
49
- )
50
- . map ( larger )
52
+ first ( largeNumbers ( xs ) )
53
+ . map ( larger )
51
54
52
- console . log (
53
- ` app1([2, 400, 5, 1000]) : `,
55
+ console . log ( `
56
+ app1([2, 400, 5, 1000]) : ` ,
54
57
app1 ( [ 2 , 400 , 5 , 1000 ] )
55
58
)
56
59
57
60
61
+
62
+ // --- Database Example --- //
63
+
58
64
// fake user returned by id for testing purposes
59
65
const fake = id => ( {
60
66
id : id ,
@@ -77,12 +83,25 @@ const eitherToTask = e =>
77
83
78
84
// valid user (id > 2)
79
85
// -> Task returns Either
80
- console . log ( `Db.find(3).chain(eitherToTask).fork(..., ...) : ` )
86
+ console . log ( `
87
+ Db.find(3)
88
+ .chain(eitherToTask)
89
+ .fork(..., ...) : `
90
+ )
91
+
92
+ // -> Task(Either(res))
81
93
Db . find ( 3 )
94
+ // -> Task(res)
82
95
. chain ( eitherToTask )
83
96
. fork ( console . error , console . log )
84
97
85
- console . log ( `Db.find(3).chain(eitherToTask).chain(user => Db.find(user.best_friend_id)).chain(eitherToTask).fork(..., ...) : ` )
98
+ console . log ( `
99
+ Db.find(3)
100
+ .chain(eitherToTask)
101
+ .chain(user => Db.find(user.best_friend_id))
102
+ .chain(eitherToTask)
103
+ .fork(..., ...) : `
104
+ )
86
105
87
106
Db . find ( 3 )
88
107
@@ -101,14 +120,17 @@ Db.find(3)
101
120
. fork ( console . error , console . log )
102
121
103
122
104
- console . log ( `Db.find(2).chain(eitherToTask).chain(user => Db.find(user.best_friend_id)).chain(eitherToTask).fork(..., ...) : ` )
123
+ console . log ( `
124
+ Db.find(2)
125
+ .chain(eitherToTask)
126
+ .chain(user => Db.find(user.best_friend_id))
127
+ .chain(eitherToTask)
128
+ .fork(..., ...) : `
129
+ )
105
130
106
131
// invalid user (id = 2)
107
132
Db . find ( 2 )
108
133
. chain ( eitherToTask )
109
134
. chain ( user => Db . find ( user . best_friend_id ) )
110
135
. chain ( eitherToTask )
111
136
. fork ( console . error , console . log )
112
-
113
-
114
-
0 commit comments