source: node_modules/ramda/src/traverse.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.1 KB
Line 
1var _curry3 =
2/*#__PURE__*/
3require("./internal/_curry3.js");
4
5var map =
6/*#__PURE__*/
7require("./map.js");
8
9var sequence =
10/*#__PURE__*/
11require("./sequence.js");
12/**
13 * Maps an [Applicative](https://github.com/fantasyland/fantasy-land#applicative)-returning
14 * function over a [Traversable](https://github.com/fantasyland/fantasy-land#traversable),
15 * then uses [`sequence`](#sequence) to transform the resulting Traversable of Applicative
16 * into an Applicative of Traversable.
17 *
18 * Dispatches to the `traverse` method of the third argument, if present.
19 *
20 * @func
21 * @memberOf R
22 * @since v0.19.0
23 * @category List
24 * @sig fantasy-land/of :: TypeRep f => f ~> a -> f a
25 * @sig (Applicative f, Traversable t) => TypeRep f -> (a -> f b) -> t a -> f (t b)
26 * @sig (Applicative f, Traversable t) => (b -> f b) -> (a -> f b) -> t a -> f (t b)
27 * @param {Object|Function} TypeRepresentative with an `of` or `fantasy-land/of` method
28 * @param {Function} f
29 * @param {*} traversable
30 * @return {*}
31 * @see R.sequence
32 * @example
33 *
34 * // Returns `Maybe.Nothing` if the given divisor is `0`
35 * const safeDiv = n => d => d === 0 ? Maybe.Nothing() : Maybe.Just(n / d)
36 *
37 * R.traverse(Maybe.of, safeDiv(10), [2, 4, 5]); //=> Maybe.Just([5, 2.5, 2])
38 * R.traverse(Maybe.of, safeDiv(10), [2, 0, 5]); //=> Maybe.Nothing
39 *
40 * // Using a Type Representative
41 * R.traverse(Maybe, safeDiv(10), Right(4)); //=> Just(Right(2.5))
42 * R.traverse(Maybe, safeDiv(10), Right(0)); //=> Nothing
43 * R.traverse(Maybe, safeDiv(10), Left("X")); //=> Just(Left("X"))
44 */
45
46
47var traverse =
48/*#__PURE__*/
49_curry3(function traverse(F, f, traversable) {
50 var of = typeof F['fantasy-land/of'] === 'function' ? F['fantasy-land/of'] : typeof F.of === 'function' ? F.of : F;
51 var TypeRep = {
52 'fantasy-land/of': of
53 };
54 return typeof traversable['fantasy-land/traverse'] === 'function' ? traversable['fantasy-land/traverse'](TypeRep, f) : typeof traversable.traverse === 'function' ? traversable.traverse(TypeRep, f) : sequence(TypeRep, map(f, traversable));
55});
56
57module.exports = traverse;
Note: See TracBrowser for help on using the repository browser.