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.

36 lines
1.3 KiB

4 years ago
  1. import _curry3 from "./internal/_curry3.js";
  2. import map from "./map.js";
  3. import sequence from "./sequence.js";
  4. /**
  5. * Maps an [Applicative](https://github.com/fantasyland/fantasy-land#applicative)-returning
  6. * function over a [Traversable](https://github.com/fantasyland/fantasy-land#traversable),
  7. * then uses [`sequence`](#sequence) to transform the resulting Traversable of Applicative
  8. * into an Applicative of Traversable.
  9. *
  10. * Dispatches to the `traverse` method of the third argument, if present.
  11. *
  12. * @func
  13. * @memberOf R
  14. * @since v0.19.0
  15. * @category List
  16. * @sig (Applicative f, Traversable t) => (a -> f a) -> (a -> f b) -> t a -> f (t b)
  17. * @param {Function} of
  18. * @param {Function} f
  19. * @param {*} traversable
  20. * @return {*}
  21. * @see R.sequence
  22. * @example
  23. *
  24. * // Returns `Maybe.Nothing` if the given divisor is `0`
  25. * const safeDiv = n => d => d === 0 ? Maybe.Nothing() : Maybe.Just(n / d)
  26. *
  27. * R.traverse(Maybe.of, safeDiv(10), [2, 4, 5]); //=> Maybe.Just([5, 2.5, 2])
  28. * R.traverse(Maybe.of, safeDiv(10), [2, 0, 5]); //=> Maybe.Nothing
  29. */
  30. var traverse =
  31. /*#__PURE__*/
  32. _curry3(function traverse(of, f, traversable) {
  33. return typeof traversable['fantasy-land/traverse'] === 'function' ? traversable['fantasy-land/traverse'](f, of) : sequence(of, map(f, traversable));
  34. });
  35. export default traverse;