1 | var _curry3 =
|
---|
2 | /*#__PURE__*/
|
---|
3 | require("./internal/_curry3.js");
|
---|
4 |
|
---|
5 | var map =
|
---|
6 | /*#__PURE__*/
|
---|
7 | require("./map.js");
|
---|
8 |
|
---|
9 | var sequence =
|
---|
10 | /*#__PURE__*/
|
---|
11 | require("./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 |
|
---|
47 | var 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 |
|
---|
57 | module.exports = traverse; |
---|