https://github.com/Gozala/reducers
Raw File
Tip revision: 291e0255e28ba6c015b69cc8556a287f9c873b9e authored by Irakli Gozalishvili on 25 October 2012, 06:36:51 UTC
Version 0.1.4
Tip revision: 291e025
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