UNPKG

1.36 kBJavaScriptView Raw
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2");
4
5var ap =
6/*#__PURE__*/
7require("./ap");
8
9var map =
10/*#__PURE__*/
11require("./map");
12
13var prepend =
14/*#__PURE__*/
15require("./prepend");
16
17var reduceRight =
18/*#__PURE__*/
19require("./reduceRight");
20/**
21 * Transforms a [Traversable](https://github.com/fantasyland/fantasy-land#traversable)
22 * of [Applicative](https://github.com/fantasyland/fantasy-land#applicative) into an
23 * Applicative of Traversable.
24 *
25 * Dispatches to the `sequence` method of the second argument, if present.
26 *
27 * @func
28 * @memberOf R
29 * @since v0.19.0
30 * @category List
31 * @sig (Applicative f, Traversable t) => (a -> f a) -> t (f a) -> f (t a)
32 * @param {Function} of
33 * @param {*} traversable
34 * @return {*}
35 * @see R.traverse
36 * @example
37 *
38 * R.sequence(Maybe.of, [Just(1), Just(2), Just(3)]); //=> Just([1, 2, 3])
39 * R.sequence(Maybe.of, [Just(1), Just(2), Nothing()]); //=> Nothing()
40 *
41 * R.sequence(R.of, Just([1, 2, 3])); //=> [Just(1), Just(2), Just(3)]
42 * R.sequence(R.of, Nothing()); //=> [Nothing()]
43 */
44
45
46var sequence =
47/*#__PURE__*/
48_curry2(function sequence(of, traversable) {
49 return typeof traversable.sequence === 'function' ? traversable.sequence(of) : reduceRight(function (x, acc) {
50 return ap(map(prepend, x), acc);
51 }, of([]), traversable);
52});
53
54module.exports = sequence;
\No newline at end of file