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
flatten.js
"use strict";

var reduce = require("./reduce")
var reducible = require("./reducible")

function flatten(source) {
  /**
  Flattens given sequence of sequences to a sequence with items of
  all nested sequences. Note that items in the resulting sequence
  are ordered by the time rather then index, in other words if
  item from the second sub-sequence is deliver earlier then the item
  from first sub-sequence it will in appear earlier in the resulting
  sequence too. If you need to keep order by index you should enforce
  sequential ordering by wrapping `source` into `sequential` before
  passing it to flatten.

  print(flatten([ [1, 2], [ 3, 4 ] ]))  // => <stream 1 2 3 4 />
  **/
  return reducible(source, function(_, next, initial) {
    return reduce(source, function(result, nested) {
      return reduce(nested, function(result, value) {
        return next(result, value)
      }, result)
    }, initial)
  })
}

module.exports = flatten
back to top