UNPKG

2.68 kBJavaScriptView Raw
1var _reduce =
2/*#__PURE__*/
3require("./internal/_reduce");
4
5var _xwrap =
6/*#__PURE__*/
7require("./internal/_xwrap");
8
9var curryN =
10/*#__PURE__*/
11require("./curryN");
12/**
13 * Initializes a transducer using supplied iterator function. Returns a single
14 * item by iterating through the list, successively calling the transformed
15 * iterator function and passing it an accumulator value and the current value
16 * from the array, and then passing the result to the next call.
17 *
18 * The iterator function receives two values: *(acc, value)*. It will be
19 * wrapped as a transformer to initialize the transducer. A transformer can be
20 * passed directly in place of an iterator function. In both cases, iteration
21 * may be stopped early with the [`R.reduced`](#reduced) function.
22 *
23 * A transducer is a function that accepts a transformer and returns a
24 * transformer and can be composed directly.
25 *
26 * A transformer is an an object that provides a 2-arity reducing iterator
27 * function, step, 0-arity initial value function, init, and 1-arity result
28 * extraction function, result. The step function is used as the iterator
29 * function in reduce. The result function is used to convert the final
30 * accumulator into the return type and in most cases is
31 * [`R.identity`](#identity). The init function can be used to provide an
32 * initial accumulator, but is ignored by transduce.
33 *
34 * The iteration is performed with [`R.reduce`](#reduce) after initializing the transducer.
35 *
36 * @func
37 * @memberOf R
38 * @since v0.12.0
39 * @category List
40 * @sig (c -> c) -> ((a, b) -> a) -> a -> [b] -> a
41 * @param {Function} xf The transducer function. Receives a transformer and returns a transformer.
42 * @param {Function} fn The iterator function. Receives two values, the accumulator and the
43 * current element from the array. Wrapped as transformer, if necessary, and used to
44 * initialize the transducer
45 * @param {*} acc The initial accumulator value.
46 * @param {Array} list The list to iterate over.
47 * @return {*} The final, accumulated value.
48 * @see R.reduce, R.reduced, R.into
49 * @example
50 *
51 * const numbers = [1, 2, 3, 4];
52 * const transducer = R.compose(R.map(R.add(1)), R.take(2));
53 * R.transduce(transducer, R.flip(R.append), [], numbers); //=> [2, 3]
54 *
55 * const isOdd = (x) => x % 2 === 1;
56 * const firstOddTransducer = R.compose(R.filter(isOdd), R.take(1));
57 * R.transduce(firstOddTransducer, R.flip(R.append), [], R.range(0, 100)); //=> [1]
58 */
59
60
61var transduce =
62/*#__PURE__*/
63curryN(4, function transduce(xf, fn, acc, list) {
64 return _reduce(xf(typeof fn === 'function' ? _xwrap(fn) : fn), acc, list);
65});
66module.exports = transduce;
\No newline at end of file