You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.3 KiB

4 years ago
  1. import _curry2 from "./internal/_curry2.js";
  2. import ap from "./ap.js";
  3. import map from "./map.js";
  4. import prepend from "./prepend.js";
  5. import reduceRight from "./reduceRight.js";
  6. /**
  7. * Transforms a [Traversable](https://github.com/fantasyland/fantasy-land#traversable)
  8. * of [Applicative](https://github.com/fantasyland/fantasy-land#applicative) into an
  9. * Applicative of Traversable.
  10. *
  11. * Dispatches to the `sequence` method of the second argument, if present.
  12. *
  13. * @func
  14. * @memberOf R
  15. * @since v0.19.0
  16. * @category List
  17. * @sig (Applicative f, Traversable t) => (a -> f a) -> t (f a) -> f (t a)
  18. * @param {Function} of
  19. * @param {*} traversable
  20. * @return {*}
  21. * @see R.traverse
  22. * @example
  23. *
  24. * R.sequence(Maybe.of, [Just(1), Just(2), Just(3)]); //=> Just([1, 2, 3])
  25. * R.sequence(Maybe.of, [Just(1), Just(2), Nothing()]); //=> Nothing()
  26. *
  27. * R.sequence(R.of, Just([1, 2, 3])); //=> [Just(1), Just(2), Just(3)]
  28. * R.sequence(R.of, Nothing()); //=> [Nothing()]
  29. */
  30. var sequence =
  31. /*#__PURE__*/
  32. _curry2(function sequence(of, traversable) {
  33. return typeof traversable.sequence === 'function' ? traversable.sequence(of) : reduceRight(function (x, acc) {
  34. return ap(map(prepend, x), acc);
  35. }, of([]), traversable);
  36. });
  37. export default sequence;