source: node_modules/ramda/src/chain.js@ d24f17c

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

Initial commit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2.js");
4
5var _dispatchable =
6/*#__PURE__*/
7require("./internal/_dispatchable.js");
8
9var _makeFlat =
10/*#__PURE__*/
11require("./internal/_makeFlat.js");
12
13var _xchain =
14/*#__PURE__*/
15require("./internal/_xchain.js");
16
17var map =
18/*#__PURE__*/
19require("./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
48var 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
62module.exports = chain;
Note: See TracBrowser for help on using the repository browser.