0.5.0 • Published 9 years ago

transduce-stream v0.5.0

Weekly downloads
93
License
MIT
Repository
github
Last release
9 years ago

Transduce Stream

Build Status

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
0.5.0

9 years ago

0.4.0

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.1.0

9 years ago

0.0.10

9 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago