https://github.com/Gozala/reducers
Raw File
Tip revision: 5d6ad72e59e1d2edc2c8275009d81b1e8a083716 authored by Irakli Gozalishvili on 25 October 2012, 02:53:20 UTC
Version 0.1.3
Tip revision: 5d6ad72
reductions.js
"use strict";

var convert = require("./convert")
var accumulate = require("./accumulate")

function reductions(source, f, initial) {
  /**
  Returns `reducible` collection of the intermediate values of the reduction
  (as per reduce) of `source` by `f`, starting with `initial` value.

  ## Example

  var numbers = reductions([1, 1, 1, 1], function(accumulated, value) {
    return accumulated + value
  }, 0)
  print(numbers) // => <stream 1 2 3 4 />
  **/
  return convert(source, function(self, next, result) {
    var state = initial
    accumulate(source, function(value, result) {
      state = value && value.isBoxed ? next(value, result) : f(state, value)
      return next(state, result)
    }, result)
  })
}

module.exports = reductions
back to top