UNPKG

2.08 kBJavaScriptView Raw
1import _clone from "./internal/_clone.js";
2import _curry3 from "./internal/_curry3.js";
3import _isTransformer from "./internal/_isTransformer.js";
4import _reduce from "./internal/_reduce.js";
5import _stepCat from "./internal/_stepCat.js";
6/**
7 * Transforms the items of the list with the transducer and appends the
8 * transformed items to the accumulator using an appropriate iterator function
9 * based on the accumulator type.
10 *
11 * The accumulator can be an array, string, object or a transformer. Iterated
12 * items will be appended to arrays and concatenated to strings. Objects will
13 * be merged directly or 2-item arrays will be merged as key, value pairs.
14 *
15 * The accumulator can also be a transformer object that provides a 2-arity
16 * reducing iterator function, step, 0-arity initial value function, init, and
17 * 1-arity result extraction function result. The step function is used as the
18 * iterator function in reduce. The result function is used to convert the
19 * final accumulator into the return type and in most cases is R.identity. The
20 * init function is used to provide the initial accumulator.
21 *
22 * The iteration is performed with [`R.reduce`](#reduce) after initializing the
23 * transducer.
24 *
25 * @func
26 * @memberOf R
27 * @since v0.12.0
28 * @category List
29 * @sig a -> (b -> b) -> [c] -> a
30 * @param {*} acc The initial accumulator value.
31 * @param {Function} xf The transducer function. Receives a transformer and returns a transformer.
32 * @param {Array} list The list to iterate over.
33 * @return {*} The final, accumulated value.
34 * @see R.transduce
35 * @example
36 *
37 * const numbers = [1, 2, 3, 4];
38 * const transducer = R.compose(R.map(R.add(1)), R.take(2));
39 *
40 * R.into([], transducer, numbers); //=> [2, 3]
41 *
42 * const intoArray = R.into([]);
43 * intoArray(transducer, numbers); //=> [2, 3]
44 */
45
46var into =
47/*#__PURE__*/
48_curry3(function into(acc, xf, list) {
49 return _isTransformer(acc) ? _reduce(xf(acc), acc['@@transducer/init'](), list) : _reduce(xf(_stepCat(acc)), _clone(acc, [], [], false), list);
50});
51
52export default into;
\No newline at end of file