Skip to content

Commit e437ada

Browse files
committed
reformat
1 parent 22347c0 commit e437ada

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

examples/25-apply-natural-transformations.js

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ const { List } = require('immutable-ext')
44

55
const Task = require('data.task')
66

7-
// 'chain' does not exist on the array,
7+
// 'chain' (aka 'flatMap') does not exist on the array,
88
// so we apply natural transformation into the List first
9+
// array -> List(array) is a natural transformation, it changes structure but not content
910
const res = List(['hello', 'world'])
1011
.chain( x => List(x.split('')) )
1112

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+
`,
1417
res
1518
)
1619

@@ -28,33 +31,36 @@ const larger = x =>
2831
x * 2
2932

3033

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
3237

38+
const app = xs =>
3339
// map, then first
3440
first(
3541
largeNumbers(xs)
36-
.map(larger)
42+
.map(larger)
3743
)
3844

39-
console.log(
40-
`app([2, 400, 5, 1000]) : `,
45+
console.log(`
46+
app([2, 400, 5, 1000]) : `,
4147
app([2, 400, 5, 1000])
4248
)
4349

4450
const app1 = xs =>
45-
4651
// first, then map
47-
first(
48-
largeNumbers(xs)
49-
)
50-
.map(larger)
52+
first(largeNumbers(xs))
53+
.map(larger)
5154

52-
console.log(
53-
`app1([2, 400, 5, 1000]) : `,
55+
console.log(`
56+
app1([2, 400, 5, 1000]) : `,
5457
app1([2, 400, 5, 1000])
5558
)
5659

5760

61+
62+
// --- Database Example --- //
63+
5864
// fake user returned by id for testing purposes
5965
const fake = id => ({
6066
id: id,
@@ -77,12 +83,25 @@ const eitherToTask = e =>
7783

7884
// valid user (id > 2)
7985
// -> 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))
8193
Db.find(3)
94+
// -> Task(res)
8295
.chain(eitherToTask)
8396
.fork(console.error, console.log)
8497

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+
)
86105

87106
Db.find(3)
88107

@@ -101,14 +120,17 @@ Db.find(3)
101120
.fork(console.error, console.log)
102121

103122

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+
)
105130

106131
// invalid user (id = 2)
107132
Db.find(2)
108133
.chain(eitherToTask)
109134
.chain(user => Db.find(user.best_friend_id))
110135
.chain(eitherToTask)
111136
.fork(console.error, console.log)
112-
113-
114-

0 commit comments

Comments
 (0)