into.js 2.0 KB

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