API Docs for: 0.4.135

Class transducers

Class defined in: src/com/cognitect/transducers.js:36
com.cognitect.transducers.ITransformer transducers.cat( xf )
src/com/cognitect/transducers.js:836

Given a transformer return a concatenating transformer

Parameters:
  • xf <com.cognitect.transducers.ITransformer>

    a transformer


Returns: <com.cognitect.transducers.ITransformer>

a concatenating transformer

transducers.comp( varArgs )
src/com/cognitect/transducers.js:250

Function composition. Take N function and return their composition.

Parameters:
  • varArgs <Function>

    N functions

Example
var t = transducers;
var inc = function(n) { return n + 1 };
var double = function(n) { return n * 2 };
var incDouble = t.comp(double, inc);
incDouble(3); // 8
Function transducers.complement( a )
src/com/cognitect/transducers.js:106

Take a predicate function and return its complement.

Parameters:
  • a <Function>

    predicate function


Returns: <Function>

the complement predicate function

Example
var isEven = function(n) { return n % 2 == 0; };
var isOdd = transducers.complement(isEven);
com.cognitect.transducers.ITransformer transducers.completing( xf, cf )
src/com/cognitect/transducers.js:1072

A completing transducer constructor. Useful to provide cleanup logic at the end of a reduction/transduction.

Parameters:
  • xf <com.cognitect.transducers.ITransformer>

    a transducer

  • cf <Function>

    a function to apply at the end of the reduction/transduction


Returns: <com.cognitect.transducers.ITransformer>

a transducer

com.cognitect.transducers.Drop transducers.drop( n )
src/com/cognitect/transducers.js:536

A dropping transducer constructor

Parameters:
  • n <Number>

    an integer, the number of inputs to drop.


Returns: <com.cognitect.transducers.Drop>

a dropping transducer

Example
var t = transducers;
var xf = t.drop(3);
t.into([], xf, [0,1,2,3,4,5]); // [3,4,5];
com.cognitect.transducers.DropWhile transducers.dropWhile( pred )
src/com/cognitect/transducers.js:580

A dropping transducer that drop inputs as long as pred is true.

Parameters:
  • pred <Function>

    a predicate function


Returns: <com.cognitect.transducers.DropWhile>

a dropWhile transducer

Example
var t = transducers;
var xf = t.dropWhile(function(n) { return n < 3; });
t.into([], xf, [0,1,2,3,4,5]); // [3,4,5];
com.cognitect.transducers.IReduced transducers.ensureReduced( x )
src/com/cognitect/transducers.js:196

Ensure that a value is reduced. If already reduced will not re-wrap.

Parameters:
  • x <Object>

    any JavaScript value


Returns: <com.cognitect.transducers.IReduced>

a reduced value.

Example
var t = transducers;
var x = t.ensureReduced(1);
var y = t.ensureReduced(x);
x === y; // true
com.cognitect.transducers.Filter transducers.filter( pred )
src/com/cognitect/transducers.js:340

Filtering transducer constructor

Parameters:
  • pred <Function>

    a predicate function


Returns: <com.cognitect.transducers.Filter>

returns a filtering transducer

Example
var t = transducers;
var isEven = function(n) { return n % 2 == 0; };
var xf = t.filter(isEven);
t.into([], xf, [0,1,2,3,4]); // [0,2,4];
com.cognitect.transducers.ITransformer transducers.first( )
src/com/cognitect/transducers.js:1118

A transformer which simply returns the first input.


Returns: <com.cognitect.transducers.ITransformer>

a transducer transformer

Object transducers.identity( x )
src/com/cognitect/transducers.js:238

Identity function.

Parameters:
  • x <Object>

    any JavaScript value


Returns: <Object>

a JavaScript value

Example
transducers.identity(1); // 1
Object transducers.into( empty, xf, coll )
src/com/cognitect/transducers.js:1028

Reduce a value into the given empty value using a transducer.

Parameters:
  • empty <String | Array | Object>

    a JavaScript collection

  • xf <com.cognitect.transducers.ITransformer>

    a transducer

  • coll <Object>

    any iterable JavaScript value: array, string, object, or iterable.


Returns: <Object>

a JavaScript value.

Example
var t = transducers;
var inc = function(n) { return n+1; };
var isEven = function(n) { return n % 2 == 0; };
var apush = function(arr,x) { arr.push(x); return arr; };
var xf = t.comp(t.map(inc),t.filter(isEven));
t.into([], xf, [1,2,3,4]); // [2,4]
Boolean transducers.isReduced( x )
src/com/cognitect/transducers.js:181

Check if a value is reduced.

Parameters:
  • x <Object>

    any JavaScript value


Returns: <Boolean>

true if the value is an instance of transducers.Reduced false otherwise

Example
var t = transducers;
t.isReduced(1); // false
t.isReduced(t.reduced(1)); // true
com.cognitect.transducers.Keep transducers.keep( f )
src/com/cognitect/transducers.js:743

A keeping transducer. Keep inputs as long as the provided function does not return null or undefined.

Parameters:
  • f <Function>

    a function


Returns: <com.cognitect.transducers.Keep>

a keep transducer

Example
var t = transducers;
var xf = t.keep(function(x) { if(typeof x == "string") return "cool"; });
t.into([], xf, [0,1,"foo",3,4,"bar"]); // ["foo","bar"]
com.cognitect.transducers.KeepIndexed transducers.keepIndexed( f )
src/com/cognitect/transducers.js:789

Like keep but the provided function will be passed the index as the second argument.

Parameters:
  • f <Function>

    a function


Returns: <com.cognitect.transducers.KeepIndexed>

a keepIndexed transducer

Example
var t = transducers;
var xf = t.keepIndexed(function(i, x) { if(typeof x == "string") return "cool"; });
t.into([], xf, [0,1,"foo",3,4,"bar"]); // ["foo","bar"]
com.cognitect.transducers.Map transducers.map( f )
src/com/cognitect/transducers.js:297

Mapping transducer constructor

Parameters:
  • f <Function>

    the mapping operation


Returns: <com.cognitect.transducers.Map>

returns a mapping transducer

Example
var t = transducers;
var inc = function(n) { return n+1; };
var xf = t.map(inc);
t.into([], xf, [1,2,3]); // [2,3,4]
com.cognitect.transducers.ITransformer transducers.mapcat( f )
src/com/cognitect/transducers.js:857

A mapping concatenating transformer

Parameters:
  • f <Function>

    the mapping function


Returns: <com.cognitect.transducers.ITransformer>

a mapping concatenating transducer

Example
var t = transducers;
var reverse = function(arr) { var arr = Array.prototype.slice.call(arr, 0); arr.reverse(); return arr; }
var xf = t.mapcat(reverse);
t.into([], xf, [[3,2,1],[6,5,4]]); // [1,2,3,4,5,6]
com.cognitect.transducers.PartitionAll transducers.partitionAll( n )
src/com/cognitect/transducers.js:699

A partitioning transducer. Collects inputs into arrays of size N.

Parameters:
  • n <Number>

    an integer


Returns: <com.cognitect.transducers.PartitionAll>

a partitionAll transducer

Example
var t = transducers;
var xf = t.partitionAll(3);
t.into([], xf, [0,1,2,3,4,5]); // [[0,1,2],[3,4,5]]
com.cognitect.transducers.PartitionBy transducers.partitionBy( f )
src/com/cognitect/transducers.js:645

A partitioning transducer. Collects inputs into arrays as long as predicate remains true for contiguous inputs.

Parameters:
  • f <Function>

    a partition function. When the result for an input changes from the previous result will create a partition.


Returns: <com.cognitect.transducers.PartitionBy>

a partitionBy transducer

Example
var t = transducers;
var xf = t.partitionBy(function(x) { return typeof x == "string"; });
t.into([], xf, [0,1,"foo","bar",2,3,"bar","baz"]); // [[0,1],["foo","bar"],[2,3],["bar","baz"]];
com.cognitect.transducers.ITransformer transducers.preservingReduced( xf )
src/com/cognitect/transducers.js:810

Given a transformer returns a transformer which preserves reduced by wrapping one more time. See cat.

Parameters:
  • xf <com.cognitect.transducers.ITransformer>

    a transformer


Returns: <com.cognitect.transducers.ITransformer>

a transformer which preserves reduced

Object transducers.reduce( xf, init, coll )
src/com/cognitect/transducers.js:954

Given a transducer, an intial value and a collection - returns the reduction.

Parameters:
  • xf <com.cognitect.transducers.ITransformer | Function>

    a transducer or two-arity function

  • init <Object>

    any JavaScript value

  • coll <String | Array | Object>

    any iterable JavaScript value


Returns: <Object>

an iterable JavaScript value: string, array iterable, or object.

com.cognitect.transducers.IReduced transducers.reduced( x )
src/com/cognitect/transducers.js:169

Return a reduced value. Reduced values short circuit transduce.

Parameters:
  • x <Object>

    any JavaScript value


Returns: <com.cognitect.transducers.IReduced>

a reduced value

Example
var reduced = transducers.reduced(1);
com.cognitect.transducers.Filter transducers.remove( pred )
src/com/cognitect/transducers.js:361

Similar to filter except the predicate is used to eliminate values.

Parameters:
  • pred <Function>

    a predicate function


Returns: <com.cognitect.transducers.Filter>

returns a removing transducer

Example
var t = transducers;
var isEven = function(n) { return n % 2 == 0; };
var xf = t.remove(isEven);
t.into([], xf, [0,1,2,3,4]); // [1,3];
com.cognitect.transducers.Take transducers.take( n )
src/com/cognitect/transducers.js:405

A take transducer constructor. Will take n values before returning a reduced result.

Parameters:
  • n <Number>

    the number of inputs to receive.


Returns: <com.cognitect.transducers.Take>

a take transducer

Example
var t = transducers;
var xf = t.take(3);
t.into([], xf, [0,1,2,3,4,5]); // [0,1,2];
com.cognitect.transducers.TakeNth transducers.takeNth( n )
src/com/cognitect/transducers.js:493

A transducer that takes every Nth input

Parameters:
  • n <Number>

    an integer


Returns: <com.cognitect.transducers.TakeNth>

a takeNth transducer

Example
var t = transducers;
var xf = t.takeNth(3);
t.into([], xf, [0,1,2,3,4,5]); // [2,5];
com.cognitect.transducers.TakeWhile transducers.takeWhile( pred )
src/com/cognitect/transducers.js:448

Like the take transducer except takes as long as the pred return true for inputs.

Parameters:
  • pred <Function>

    a predicate function


Returns: <com.cognitect.transducers.TakeWhile>

a takeWhile transducer

Example
var t = transducers;
var xf = t.takeWhile(function(n) { return n < 3; });
t.into([], xf, [0,1,2,3,4,5]); // [0,1,2];
Function transducers.toFn( xf, builder )
src/com/cognitect/transducers.js:1090

Convert a transducer transformer object into a function so that it can be used with existing reduce implementation i.e. native, Underscore, lodash

Parameters:
  • xf <com.cognitect.transducers.ITransformer>

    a transducer

  • builder <Function>

    a function which take the accumulator and the the next input and return a new accumulator value.


Returns: <Function>

a two-arity function compatible with existing reduce implementations

Example
var t = transducers;
var arr = [0,1,2,3,4,5],
var apush = function(arr, x) { arr.push(x); return arr; },
var xf = t.comp(t.map(inc),t.filter(isEven));
arr.reduce(t.toFn(xf, apush), []); // [2,4,6]
Object transducers.transduce( xf, f, init, coll )
src/com/cognitect/transducers.js:981

Given a transducer, a builder function, an initial value and a iterable collection - returns the reduction. collection - returns the reduction.

Parameters:
  • xf <com.cognitect.transducers.ITransformer>

    a transducer

  • f <com.cognitect.transducers.ITransformer | Function>

    a transducer or two-arity function

  • init <Object=>

    any JavaScript value

  • coll <String | Array | Object>

    any iterable JavaScript value


Returns: <Object>

a JavaScript value.

Example
var t = transducers;
var inc = function(n) { return n+1; };
var isEven = function(n) { return n % 2 == 0; };
var apush = function(arr,x) { arr.push(x); return arr; };
var xf = t.comp(t.map(inc),t.filter(isEven));
t.transduce(xf, apush, [], [1,2,3,4]); // [2,4]
Object transducers.unreduced( x )
src/com/cognitect/transducers.js:219

Ensure a value is not reduced. Unwraps if reduced.

Parameters:
  • x <Object>

    any JavaScript value


Returns: <Object>

a JavaScript value

Example
var t = transducers;
var x = t.reduced(1);
t.unreduced(x); // 1
t.unreduced(t.unreduced(x)); // 1
com.cognitect.transducers.Wrap transducers.wrap( stepFn )
src/com/cognitect/transducers.js:138

Take a two-arity reducing function where the first argument is the accumluation and the second argument is the next input and convert it into a transducer transformer object.

Parameters:
  • stepFn <Function>

    a two-arity reducing function


Returns: <com.cognitect.transducers.Wrap>

a transducer transformer object

Example
var t = transducers;
var arrayPush = t.wrap(function(arr, x) { arr.push(x); return arr; });