-
-
Save Raynos/3692584 to your computer and use it in GitHub Desktop.
Flatten example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var from = require("read-stream") | |
, to = require("write-stream") | |
, flatten = require("..") | |
, map = require("lazy-map-stream") | |
, assert = require("assert") | |
, list = [] | |
// expand(stream, iterator) -> flatten(map(stream, iterator)) | |
var flattened = expand(from([ | |
[1,2,3] | |
, [4,5,6] | |
, [7,8,9] | |
]), function (list) { | |
return from(list) | |
}) | |
flattened.pipe(to(list, function () { | |
// The flattened values | |
assert.deepEqual(list, [1,2,3,4,5,6,7,8,9]) | |
console.log("list", list) | |
})) | |
// flatten(stream) | |
// Assumes the stream contains chunk which are themself streams | |
// it then flattens out all of the chunks from the streams into one stream | |
function expand(stream, iterator) { | |
return flatten(map(stream, iterator)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment