transduce-stream
v0.5.0
Published
Transform Stream with Transducers
Downloads
130
Readme
Transduce Stream
Transform Node.js streams with transducers.
Works with transducers-js or transducers.js or transduce.
var stream = require('transduce-stream')
var transducer = // create transducer with transduce, transducers-js or transducers.js
process.stdin.resume()
process.stdin.pipe(stream(transducer)).pipe(process.stdout)
Example:
var stream = require('./'),
tr = require('transduce') // or transducers-js or transducers.js
var trans = tr.compose(
tr.string.words(),
tr.map(function(x){return (+x * +x) + ' '}),
tr.array.push('\n'))
process.stdin.resume()
process.stdin.pipe(stream(trans)).pipe(process.stdout)
Run to get the squares of numbers passed on stdin
.
$ echo '1 12 7 41' | node square.js
1 144 49 1681
This example makes use of transduce/array to add a new line at the end of the stream and transduce/string to split the input on words (can also split on lines, chars and separators or RegExps).
Or using underarm.
// test.js
var _r = require('underarm')
stream = require('transduce-stream')
var transducer = _r()
.words()
.map(function(x){return (+x * +x)+ ' '})
.uniq()
.take(4)
.push('\n')
.compose()
process.stdin.resume()
process.stdin.pipe(stream(transducer)).pipe(process.stdout)
Run this from the terminal to calculate a formatted sequence of the first 4 unique squared values.
$ echo '33 27 33 444' | node test.js
1089 729 197136
$ node test.js << EOT
12 32
33 33
33 43
12 33 12
EOT
144 1024 1089 1849