Skip to content

Commit 5f85f4f

Browse files
committed
cleanup traverse
1 parent 87d1b6c commit 5f85f4f

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

examples/12-creed.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ launchMissilesPromise()
2121
.then( console.log )
2222

2323

24+
const suspendedMissile = name => ({
25+
first: _ => console.log(`${name} missile is ready...`),
26+
then: _ => console.log(`${name} missile is launched!!!`)
27+
})
2428

25-
const suspendedLaunchCreed = _ =>
26-
// return suspended missile inside function,
27-
CreedPromise.of({then: _ => {
28-
console.log("Creed missile is launched!!!")
29-
}})
29+
const suspendedLaunchCreed = CreedPromise.of(suspendedMissile('Creed'))
3030

3131
const suspendedLaunchPromise = _ =>
3232
// return suspended missile inside function,
3333
Promise.resolve({then: _ => {
3434
console.log("Promise missile is launched!!!")
3535
}})
3636

37-
suspendedLaunchCreed()
37+
suspendedLaunchCreed
3838
// execute instruction
3939
.then(instruction => instruction.then())
4040

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
// Native
12
const fs = require('fs')
23

4+
// Packages
35
const Task = require('data.task')
46
const { List } = require('immutable-ext')
57

@@ -14,14 +16,25 @@ const readFileTask = FutureTask(fs.readFile)
1416
const files = List(['config.json', 'config1.json'])
1517

1618
console.log(
17-
`List(['config.json', 'config1.json']).traverse(Task.of, fn => readFileTask(fn, 'utf-8')).fork(..., ...) : `
19+
`List(['config.json', 'config1.json'])
20+
.traverse(Task.of, fn => readFileTask(fn, 'utf-8'))
21+
.fork(..., ...) : `
1822
)
1923

20-
files
24+
/*
25+
'files' is List of files 'List(a)'
26+
'map' preserves the List wrapper, so we can get List of Tasks 'List(Task(a))'
27+
'traverse' applies the function (a -> f b) to each List entry,
28+
and wraps all together into single Task of Lists,
29+
running the tasks in parallel
30+
*/
2131

22-
// we have list of files but want task of lists
23-
// 1st argument 'Task.of' lifts to Task - applicative functor
24-
// (needed as type hint in case of failure or never running the function)
25-
// 2nd argument is traversing function a -> f b
26-
.traverse( Task.of, fn => readFileTask(fn, 'utf-8') )
32+
files
33+
.traverse(
34+
// type hint, applicative functor
35+
// needed in case of failure or never running the function
36+
Task.of,
37+
// traversing function a -> f b
38+
fn => readFileTask(fn, 'utf-8')
39+
)
2740
.fork(console.error, console.log)

0 commit comments

Comments
 (0)