Class transducers
Class defined in:src/com/cognitect/transducers.js:36
transducers.cat
( xf
)
src/com/cognitect/transducers.js:836
Given a transformer return a concatenating transformer
Parameters:-
xf
<com.cognitect.transducers.ITransformer>a transformer
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
transducers.complement
( a
)
src/com/cognitect/transducers.js:106
Take a predicate function and return its complement.
Parameters:-
a
<Function>predicate function
the complement predicate function
Example
var isEven = function(n) { return n % 2 == 0; };
var isOdd = transducers.complement(isEven);
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
a transducer
transducers.drop
( n
)
src/com/cognitect/transducers.js:536
A dropping transducer constructor
Parameters:-
n
<Number>an integer, the number of inputs to 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];
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
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];
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
a reduced value.
Example
var t = transducers;
var x = t.ensureReduced(1);
var y = t.ensureReduced(x);
x === y; // true
transducers.filter
( pred
)
src/com/cognitect/transducers.js:340
Filtering transducer constructor
Parameters:-
pred
<Function>a predicate function
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];
transducers.first
(
)
src/com/cognitect/transducers.js:1118
A transformer which simply returns the first input.
a transducer transformer
transducers.identity
( x
)
src/com/cognitect/transducers.js:238
Identity function.
Parameters:-
x
<Object>any JavaScript value
a JavaScript value
Example
transducers.identity(1); // 1
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.
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]
transducers.isReduced
( x
)
src/com/cognitect/transducers.js:181
Check if a value is reduced.
Parameters:-
x
<Object>any JavaScript value
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
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
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"]
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
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"]
transducers.map
( f
)
src/com/cognitect/transducers.js:297
Mapping transducer constructor
Parameters:-
f
<Function>the mapping operation
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]
transducers.mapcat
( f
)
src/com/cognitect/transducers.js:857
A mapping concatenating transformer
Parameters:-
f
<Function>the mapping function
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]
transducers.partitionAll
( n
)
src/com/cognitect/transducers.js:699
A partitioning transducer. Collects inputs into arrays of size N.
Parameters:-
n
<Number>an integer
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]]
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.
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"]];
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
a transformer which preserves reduced
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
an iterable JavaScript value: string, array iterable, or object.
transducers.reduced
( x
)
src/com/cognitect/transducers.js:169
Return a reduced value. Reduced values short circuit transduce.
Parameters:-
x
<Object>any JavaScript value
a reduced value
Example
var reduced = transducers.reduced(1);
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 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];
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.
a take transducer
Example
var t = transducers;
var xf = t.take(3);
t.into([], xf, [0,1,2,3,4,5]); // [0,1,2];
transducers.takeNth
( n
)
src/com/cognitect/transducers.js:493
A transducer that takes every Nth input
Parameters:-
n
<Number>an integer
a takeNth transducer
Example
var t = transducers;
var xf = t.takeNth(3);
t.into([], xf, [0,1,2,3,4,5]); // [2,5];
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
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];
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.
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]
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
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]
transducers.unreduced
( x
)
src/com/cognitect/transducers.js:219
Ensure a value is not reduced. Unwraps if reduced.
Parameters:-
x
<Object>any JavaScript value
a JavaScript value
Example
var t = transducers;
var x = t.reduced(1);
t.unreduced(x); // 1
t.unreduced(t.unreduced(x)); // 1
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
a transducer transformer object
Example
var t = transducers;
var arrayPush = t.wrap(function(arr, x) { arr.push(x); return arr; });