@@ -3,30 +3,46 @@ const { Box, Either, Right, Left, fromNullable } = require('../examples/lib')
33const 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
922const 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
1529const res1 = boxToEither ( Box ( 100 ) )
1630 . map ( x => x * 2 )
1731
18- // first map, then transform
32+ // or first map, then transform to Either
1933const res2 = boxToEither (
2034 Box ( 100 ) . map ( x => x * 2 )
2135)
2236
37+ // results should be the same!
2338console . log ( res1 , res2 )
2439
2540
2641
2742const 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(
4258console . log ( res21 , res22 )
4359
4460
45-
61+ // transform List into Either
4662const first = xs =>
4763 fromNullable ( xs [ 0 ] )
4864
0 commit comments