Socket
Book a DemoInstallSign in
Socket

transduce-stream

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

transduce-stream

Transform Stream with Transducers

latest
Source
npmnpm
Version
0.5.0
Version published
Maintainers
1
Created
Source

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

Keywords

stream

FAQs

Package last updated on 04 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts