Skip to content

Commit 22347c0

Browse files
committed
add creed traverse example
1 parent 5f85f4f commit 22347c0

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

examples/12-creed.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// https://github.com/briancavalier/creed
12
const { Promise: CreedPromise } = require('creed')
23

34
const launchMissilesCreed = _ =>

examples/22-creed-traversable.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Native
2+
const fs = require('fs')
3+
4+
// Packages
5+
const { List } = require('immutable-ext')
6+
7+
// https://github.com/briancavalier/creed
8+
// runNode converts Node API with errback into creed promise
9+
const { runNode, Promise: CreedPromise } = require('creed')
10+
11+
12+
// readFilePromise :: String -> String -> Promise Error Buffer
13+
const readFilePromise = (fileName, encoding = 'utf-8') =>
14+
runNode(fs.readFile, fileName, {encoding})
15+
16+
// wrap into List that provides 'traverse'
17+
const files = List(['file1.txt', 'file2.txt'])
18+
19+
console.log(
20+
`Running...
21+
List(['file1.txt', 'file2.txt'])
22+
.traverse( CreedPromise.of, readFilePromise )
23+
.then( console.log, console.error ) :
24+
`
25+
)
26+
27+
/*
28+
'files' is List of files 'List(a)'
29+
'map' preserves the List wrapper, so we can get 'List(Promise(a))'
30+
'traverse' applies the function (a -> f b) to each List entry,
31+
then lifts the List to Promise of Lists via CreedPromise's 'ap' operator
32+
running Promises in parallel!
33+
*/
34+
35+
files
36+
.traverse(
37+
38+
// type hint, applicative functor
39+
// needed in case of failure or never running the function
40+
CreedPromise.of,
41+
42+
// traversing function a -> f b
43+
file => readFilePromise(file)
44+
)
45+
.then( console.log, console.error )

examples/22-leapfrogging-types-traversables.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { List } = require('immutable-ext')
99
const FutureTask = require('futurize').futurize(Task)
1010

1111
// (lazy) task to read file
12-
// FutureTask simply applies to the callback-based function inside
12+
// FutureTask wraps the callback-based function inside into Task
1313
const readFileTask = FutureTask(fs.readFile)
1414

1515
// need to wrap into List that provides 'traverse'
@@ -25,7 +25,7 @@ console.log(
2525
'files' is List of files 'List(a)'
2626
'map' preserves the List wrapper, so we can get List of Tasks 'List(Task(a))'
2727
'traverse' applies the function (a -> f b) to each List entry,
28-
and wraps all together into single Task of Lists,
28+
then lifts the List to Task of Lists via Task's 'ap' operator
2929
running the tasks in parallel
3030
*/
3131

examples/file1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello

examples/file2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
World!

0 commit comments

Comments
 (0)