Skip to content

Commit 27348ff

Browse files
committed
Lesson 24 rename
1 parent b15bbbb commit 27348ff

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

examples/24-natural-transformations.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,46 @@ const { Box, Either, Right, Left, fromNullable } = require('../examples/lib')
33
const Task = require('data.task')
44

55

6+
const eitherToTask = e =>
7+
e.fold(Task.rejected, Task.of)
68

9+
eitherToTask(Right('nightingale'))
10+
.fork(
11+
e => console.error('Error: ', e),
12+
res => console.log('Result: ', res)
13+
)
14+
eitherToTask(Left('errrr'))
15+
.fork(
16+
e => console.error('Error: ', e),
17+
res => console.log('Result: ', res)
18+
)
719

820

21+
// naturally transform Box to Either
922
const boxToEither = b =>
1023

1124
// must go to Right
1225
// to follow the natural transformation law
1326
b.fold(Right)
1427

28+
// first transform to Either, than map
1529
const res1 = boxToEither(Box(100))
1630
.map( x => x * 2 )
1731

18-
// first map, then transform
32+
// or first map, then transform to Either
1933
const res2 = boxToEither(
2034
Box(100).map( x => x * 2 )
2135
)
2236

37+
// results should be the same!
2338
console.log(res1, res2)
2439

2540

2641

2742
const boxToEitherBad = b =>
2843

2944
// does not follow the natural transformation law
45+
// because Left ignores mapped functions
3046
b.fold(Left)
3147

3248
// first box, then map
@@ -42,7 +58,7 @@ const res22 = boxToEither(
4258
console.log(res21, res22)
4359

4460

45-
61+
// transform List into Either
4662
const first = xs =>
4763
fromNullable(xs[0])
4864

0 commit comments

Comments
 (0)