Code Monkey home page Code Monkey logo

through2-reduce's Introduction

through2-reduce

NPM

through2-reduce is a thin wrapper around through2 that works like Array.prototype.reduce but for streams.

This is a much less common use-case with streams, but it can occasionally be useful to do a Reduce function on a stream.

EXPERIMENTAL This is a bit of a bizarre one, so I wouldn't be surprised if there are some dangerous edge cases around flushing and pausing and such. Use at your own risk.

This stream will only ever emit a single chunk. For more traditional stream.Transform filters or transforms, consider through2 through2-filter or through2-map.

Also, if your stream never ends, Reduce will never end.

var reduce = require("through2-reduce")

var sum = reduce({objectMode: true}, function (previous, current) { return previous + current })

// vs. with through2:
function combine (value, encoding, callback) {
  if (this.total == undefined) {
    this.total = value
    return callback()
  }
  this.total += value
  return callback()
}
function flush (callback) {
  this.push(this.total)
  return callback()
}
var sum = through2({objectMode: true}, combine, flush)

// Then use your reduce: (e.g. source is an objectMode stream of numbers)
source.pipe(sum).pipe(sink)

// Works like `Array.prototype.reduce` meaning you can specify a function that
// takes up to three* arguments: fn(previous, current, index) AND you can specify
// an initial value
var mean = reduce({objectMode: true}, function (prev, curr, index) {
  return prev - (prev - curr) / (index + 1)
}, 0)

*Differences from Array.prototype.reduce:

  • No fourth array callback argument. That would require realizing the entire stream, which is generally counter-productive to stream operations.
  • Array.prototype.reduce doesn't modify the source Array, which is somewhat nonsensical when applied to streams.

API

reduce([options,] fn [,initial])

Create a Reduce instance

reduce.ctor([options,] fn [,initial])

Create a Reduce class

reduce.obj([options,] fn [,initial])

Create a Reduce instance that defaults to objectMode: true.

reduce.objCtor([options,] fn [,initial])

Just like ctor, but with objectMode: true defaulting to true.

Options

  • wantStrings: Automatically call chunk.toString() for the super lazy.
  • all other through2 options

LICENSE

MIT

through2-reduce's People

Contributors

brycebaril avatar tjconcept avatar kacarstensen-shift avatar

Watchers

xlitter avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.