Socket
Book a DemoInstallSign in
Socket

transduce-gen

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

transduce-gen

Create Transducers with ES6 Generators

latest
Source
npmnpm
Version
0.4.0
Version published
Maintainers
1
Created
Source

Transduce Gen

Build Status

Create Transducers using ES6 Generators. Generator is created with a step function and each item in transformation is yielded through the generator. Calling step function will step value to next transformer in the pipeline. An optional completing function can be provided that will be called on transformer result.

Works with transduce, transducers-js or transducers.js.

Requires support for ES6 generators. If using Node.js run with node --harmony and version 0.11 or better.

Examples

Mapping transducer loops infinitely and transforms each yieled item with mapping function.

Map

var gen = require('transduce-gen');
function map(f){
  return gen(function*(step){
    while(true){
      step(f(yield 0));
    }
  });
}

var doubled = map(function(num){ return num * 2; });
tr.into([], doubled, [1,2,3]);
// [2,4,6]

Filter

Filter only calls step if it passes a predicate.

var gen = require('transduce-gen');
function filter(p){
  return gen(function*(step){
    while(true){
      var val = yield 0;
      if(p(val)){
        step(val);
      }
    }
  });
}
function isEven(x){
  return x%2 === 0;
}
var evenArray = [1, 2, 3, 4, 5, 6];
tr.into([], filter(isEven), evenArray);
// [2,4,6]

Take

Early termination (reduced) can be signalled by completing generator function.

var gen = require('transduce-gen');
function take(n){
  return gen(function*(step){
    var i = 0;
    for(; i < n; i++){
      step(yield 0);
    }
  });
}

tr.into([], take(2), [1, 2, 3]))
// [1, 2]

Push

Provide optional completing function to be called on result.

var gen = require('transduce-gen');
function push(){
  var args = slice.call(arguments);
  return gen(function*(step){
    while(true){
        step(yield 0);
    }
  },
  function(step){
    args.forEach(step);
  });
}
tr.into([], comp(push(4), push(5,6)), [1,2,3]))
// [1,2,3,4,5,6]

Keywords

transducer

FAQs

Package last updated on 04 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts