source: node_modules/ramda/es/chain.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: 1.4 KB
RevLine 
[d24f17c]1import _curry2 from "./internal/_curry2.js";
2import _dispatchable from "./internal/_dispatchable.js";
3import _makeFlat from "./internal/_makeFlat.js";
4import _xchain from "./internal/_xchain.js";
5import map from "./map.js";
6/**
7 * `chain` maps a function over a list and concatenates the results. `chain`
8 * is also known as `flatMap` in some libraries.
9 *
10 * Dispatches to the `chain` method of the second argument, if present,
11 * according to the [FantasyLand Chain spec](https://github.com/fantasyland/fantasy-land#chain).
12 *
13 * If second argument is a function, `chain(f, g)(x)` is equivalent to `f(g(x), x)`.
14 *
15 * Acts as a transducer if a transformer is given in list position.
16 *
17 * @func
18 * @memberOf R
19 * @since v0.3.0
20 * @category List
21 * @sig Chain m => (a -> m b) -> m a -> m b
22 * @param {Function} fn The function to map with
23 * @param {Array} list The list to map over
24 * @return {Array} The result of flat-mapping `list` with `fn`
25 * @example
26 *
27 * const duplicate = n => [n, n];
28 * R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3]
29 *
30 * R.chain(R.append, R.head)([1, 2, 3]); //=> [1, 2, 3, 1]
31 */
32
33var chain =
34/*#__PURE__*/
35_curry2(
36/*#__PURE__*/
37_dispatchable(['fantasy-land/chain', 'chain'], _xchain, function chain(fn, monad) {
38 if (typeof monad === 'function') {
39 return function (x) {
40 return fn(monad(x))(x);
41 };
42 }
43
44 return _makeFlat(false)(map(fn, monad));
45}));
46
47export default chain;
Note: See TracBrowser for help on using the repository browser.