https://github.com/Gozala/reducers
Revision 4e743321440a426d44c6ce03c05339e73c4517a5 authored by Irakli Gozalishvili on 24 October 2012, 19:47:47 UTC, committed by Irakli Gozalishvili on 24 October 2012, 19:47:47 UTC
1 parent c393dc3
Raw File
Tip revision: 4e743321440a426d44c6ce03c05339e73c4517a5 authored by Irakli Gozalishvili on 24 October 2012, 19:47:47 UTC
Version 0.1.2
Tip revision: 4e74332
transform.js
/*jshint asi: true undef: true es5: true node: true browser: true devel: true
         forin: true latedef: false globalstrict: true */

"use strict";

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

function transform(source, f) {
  /**
  Function takes `source` sequence and an `f` function, which is used to
  intercept each non-boxed item of the sequence. It is invoked with `next`
  continuation function, sequence `value` being yielded and accumulated
  `result`. `f` is supposed to call `next` with a intercepted `value` and
  `result`.

  This enables writing transformation functions that don't deal with
  back-channel in form of boxed values. In other words transformation
  functions like `filter` and `map` just intercept values but they don't
  change size of sequence or handle errors there for they want to bypass
  back-channel by using transform`.

  For examples of use see `map`, `filter` etc... Some functions that want
  to change size of sequence should use `transformer` instead, for examples
  of those see `take`, `takeWhile`.
  **/
  return convert(source, function(self, next, initial) {
    accumulate(source, function(value, result) {
      return value && value.isBoxed ? next(value, result)
                                    : f(next, value, result)
    }, initial)
  })
}

module.exports = transform
back to top