https://github.com/Gozala/reducers
Raw File
Tip revision: 2a5706e16ddbd76deabdd176b570f97984b6f153 authored by Irakli Gozalishvili on 10 November 2012, 06:03:09 UTC
Version 1.0.2
Tip revision: 2a5706e
reductions.js
"use strict";

var reducible = require("./reducible")
var accumulate = require("./accumulate")
var end = require("./end")
var isError = require("./is-error")

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) // => < 1 2 3 4 >
  **/
  return reducible(function reduceReducible(next, start) {
    var state = initial
    return accumulate(source, function reduceSource(value, result) {
      if (value === end) return next(end, result)
      if (isError(value)) return next(value, result)
      state = f(state, value)
      return next(state, result)
    }, start)
  })
}

module.exports = reductions
back to top