-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.js
65 lines (45 loc) · 2.68 KB
/
examples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
This file has some examples of using the array functions
TODO add examples of the concurrency functions.
*/
process.env.DEBUG="parallel-streams:*"; //Get debugging on all streams
const ParallelStream = require('./index.js');
/*
ParallelStream.from([0,1,2,3], {name: "Munching"}) // Create a stream called "Munching" that will output the items 0,1,2,3
.log(m=>[m], {name:"Stream 0..3"}); // 0 1 2 3 Log each item in the stream before passing it on.
// Now add a map function that squares each item in the stream
ParallelStream.from([0,1,2,3]) .map(m=>m**2) .log(m=>[m], {name:"Stream 0,1,4,9"})
// If our stream has arrays, then you can flatten them into a stream of their elements
ParallelStream.from([[0,"a"],[1,"b"]]) .log(m=>[m], {name:"Stream [0,a],[1,b]"}) .flatten() .log(m=>[m], {name:"Stream 0,a,1,b"})
// And you can filter
ParallelStream.from([0,1,2,3,4]) .filter(m=>m>1 && m<4) .log(m=>[m], {name:"Stream filter 2,3"})
// Or select uniq items
ParallelStream.from([0,1,2,2,1]) .uniq() .log(m=>[m], {name:"Stream uniq 0,1,2"})
// Or slice out a range
ParallelStream.from([0,1,2,3,4]) .slice(2,4) .log(m=>[m], {name:"Stream slice 2,3"})
// A little more complexity allows forking a stream and running two or more sets of processing on it.
let foo = ParallelStream.from([0,1])
.fork(s=>s.log(m=>[m], {name: "ForkA 0,1"}))
.log(m=>[m], {name: "ForkB 0,1"});
// Reduce works, but note that you have to use the 'function' syntax instead of (a,b)=>(a+b) if you want to use "this" for debugging.
// The result here should be 110 as 0 is used as the initial value
ParallelStream.from([10,20,30,40]) .reduce(function(acc,d,i) { return (acc + i + d+1) }, 0, function(res) {this.debug("SUM=", res)}, { name: "Sum=110" });
// The result here should be 110 as it runs reduce 3 times, with the 10 is used as initial value.
ParallelStream.from([10,20,30,40]) .reduce(function(acc,d,i) { return (acc + i + d+1) }, undefined, function(res) {this.debug("SUM=", res)}, { name: "Sum=109" });
// Reduce with no arguments is useful at the end of a chain of streams to avoid the last stream pushing back when it can't write.
ParallelStream.from([10,20,30,40]) .reduce();
*/
// Showing how you can pipe two streams into one
/*
let foo = ParallelStream.from([0,1,2,3,4,5,6])
let bar = ParallelStream.from([10,11,12,13,14,15,16])
let baz = ParallelStream.log(m=>m,{name:"splat"})
foo.pipe(baz)
bar.pipe(baz)
baz.reduce(); // Terminate
*/
let foo1 = ParallelStream.from([0,1,2,3,4,5,6])
let foo2 = ParallelStream.from([10,11,12,13,14,15,16])
let foos = ParallelStream.from([ foo1, foo2]) // Stream of streams
let flat = foos.flatten().log(m=>m).reduce();
//TODO build merge that will take a stream of streams (like flatten).