1 | var _curry2 =
|
---|
2 | /*#__PURE__*/
|
---|
3 | require("./internal/_curry2.js");
|
---|
4 |
|
---|
5 | var _dispatchable =
|
---|
6 | /*#__PURE__*/
|
---|
7 | require("./internal/_dispatchable.js");
|
---|
8 |
|
---|
9 | var _makeFlat =
|
---|
10 | /*#__PURE__*/
|
---|
11 | require("./internal/_makeFlat.js");
|
---|
12 |
|
---|
13 | var _xchain =
|
---|
14 | /*#__PURE__*/
|
---|
15 | require("./internal/_xchain.js");
|
---|
16 |
|
---|
17 | var map =
|
---|
18 | /*#__PURE__*/
|
---|
19 | require("./map.js");
|
---|
20 | /**
|
---|
21 | * `chain` maps a function over a list and concatenates the results. `chain`
|
---|
22 | * is also known as `flatMap` in some libraries.
|
---|
23 | *
|
---|
24 | * Dispatches to the `chain` method of the second argument, if present,
|
---|
25 | * according to the [FantasyLand Chain spec](https://github.com/fantasyland/fantasy-land#chain).
|
---|
26 | *
|
---|
27 | * If second argument is a function, `chain(f, g)(x)` is equivalent to `f(g(x), x)`.
|
---|
28 | *
|
---|
29 | * Acts as a transducer if a transformer is given in list position.
|
---|
30 | *
|
---|
31 | * @func
|
---|
32 | * @memberOf R
|
---|
33 | * @since v0.3.0
|
---|
34 | * @category List
|
---|
35 | * @sig Chain m => (a -> m b) -> m a -> m b
|
---|
36 | * @param {Function} fn The function to map with
|
---|
37 | * @param {Array} list The list to map over
|
---|
38 | * @return {Array} The result of flat-mapping `list` with `fn`
|
---|
39 | * @example
|
---|
40 | *
|
---|
41 | * const duplicate = n => [n, n];
|
---|
42 | * R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3]
|
---|
43 | *
|
---|
44 | * R.chain(R.append, R.head)([1, 2, 3]); //=> [1, 2, 3, 1]
|
---|
45 | */
|
---|
46 |
|
---|
47 |
|
---|
48 | var chain =
|
---|
49 | /*#__PURE__*/
|
---|
50 | _curry2(
|
---|
51 | /*#__PURE__*/
|
---|
52 | _dispatchable(['fantasy-land/chain', 'chain'], _xchain, function chain(fn, monad) {
|
---|
53 | if (typeof monad === 'function') {
|
---|
54 | return function (x) {
|
---|
55 | return fn(monad(x))(x);
|
---|
56 | };
|
---|
57 | }
|
---|
58 |
|
---|
59 | return _makeFlat(false)(map(fn, monad));
|
---|
60 | }));
|
---|
61 |
|
---|
62 | module.exports = chain; |
---|