How to use the chain function from ramda

Find comprehensive JavaScript ramda.chain code examples handpicked from public code repositorys.

The Ramda chain function applies a given function to a value and then flattens the result by one level.

34
35
36
37
38
39
40
41
42
43
44
45
46
	R.map(R.replace(/^R[.]/, ''))
);


const titleFilter = pipe(R.propEq(R.__, 'title'), R.filter);


const valueProp = R.chain(prop('value'));


const simplifyData = R.applySpec({
	aka: pipe(
		prop('tags'),
fork icon24
star icon163
watch icon0

26
27
28
29
30
31
32
33
34
35
}

return R.pipe(
  R.length,
  R.range(0),
  R.chain((i) =>
    R.pipe(
      R.remove,
      permutations,
      R.map((perm) => [R.nth(i, array), ...perm]),
fork icon0
star icon1
watch icon0

+ 4 other calls in file

How does ramda.chain work?

ramda.chain is a function in the Ramda library that applies a function to each element of an array and flattens the result into a single array. More specifically, it takes a function that returns an array as an argument, applies that function to each element of the input array, and then concatenates the resulting arrays into a single output array. It is similar to the flatMap function in other functional programming libraries. This can be useful when working with arrays of arrays, as it allows you to apply a function to each inner array and then flatten the results into a single array.

4
5
6
7
8
9
10
11
12
13
const parseInput = R.pipe(
  parseLines,
  R.map(
    R.pipe(
      R.split('\n'),
      R.chain(
        R.pipe(
          R.split(' -> '),
          R.map(R.pipe(R.split(','), R.map(parseDecimal))),
        ),
fork icon0
star icon1
watch icon0

26
27
28
29
30
31
32
33
34
35
36


function findValidFiles(source, transformers) {
  return R.pipe(
    R.reject(shouldBeIgnore),
    R.filter(R.either(isDirectory, isValidFile(transformers))),
    R.chain((filename) => {
      if (isDirectory(filename)) {
        const subFiles = fs.readdirSync(filename)
          .map(subFile => path.join(filename, subFile));
        return findValidFiles(subFiles, transformers);
fork icon0
star icon0
watch icon1

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
const R = require("ramda");

const addOne = (x) => [x + 1];
const addTwo = (x) => [x + 2];

const result = R.chain(addOne, [1, 2, 3]);
console.log(result); // [2, 3, 4]

const result2 = R.chain(addTwo, result);
console.log(result2); // [4, 5, 6]

In this example, ramda.chain is used to combine two functions that return arrays into one function that returns a flattened array. The addOne function takes a number and returns an array with that number plus one. The addTwo function takes a number and returns an array with that number plus two. The R.chain function is used to apply the addOne function to the array [1, 2, 3] and return a new array [2, 3, 4]. Then, the addTwo function is applied to the result of the previous call to R.chain, which returns a new array [4, 5, 6].

13
14
15
16
17
18
19
20
21
22
23


/**
 * R.addIndex Should work with F(fn, [...args], [list])
 * eg:
 * R.all, R.any, R.none
 * R.chain
 * R.drop, R.dropWhile
 * R.filter, R.reject
 * R.find...
 * R.forEach
fork icon0
star icon0
watch icon0

+ 2 other calls in file

7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
 * @param {Array} list
 * @return {Array}
 * @example
 *
 *      var duplicate = n => [n, n];
 *      R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3]
 */
var chain = _curry2(_dispatchable('chain', _xchain, function chain(fn, monad) {
    if (typeof monad === 'function') {
        return function () {
fork icon0
star icon0
watch icon0

+ 53 other calls in file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
"use strict";


var R = require('ramda');


var square = function square (x) { return x * x; }
var squares = R.chain(square, [1, 2, 3, 4, 5]);


document.getElementById('response').innerHTML = squares;


},{"ramda":2}],2:[function(require,module,exports){
fork icon0
star icon0
watch icon0

+ 75 other calls in file

82
83
84
85
86
87
88
89
90
91
92
  R.pathEq(['init', 'arguments', '0', 'type'], 'Literal')
]);


//  warnIgnoredTopLevel :: {*} -> {*}
var warnIgnoredTopLevel = R.tap(R.pipe(
  R.chain(R.ifElse(R.propEq('type', 'VariableDeclaration'),
                   R.prop('declarations'),
                   R.of)),
  R.reject(R.either(isModuleExportsExpr, isRequireExpr)),
  R.map(function(ast) {
fork icon0
star icon0
watch icon0

39
40
41
42
43
44
45
46
47
48
49
  R.over(R.lensIndex(1), R.inc), 
  R.sort(R.subtract), 
)


const createWall = R.compose(
  R.chain(R.converge(R.xprod, [
    R.compose(
      lineComponents,
      R.map(R.head)
    ),
fork icon0
star icon0
watch icon0

+ 5 other calls in file

52
53
54
55
56
57
58
59
60
61
62
63
const getIndicies = (board) => {
  const height = R.length(board);
  if (height === 0) return [];
  const width = R.length(board[0]);


  return R.chain((i) => R.map(R.pair(i), R.range(0, width)),
      R.range(0, height));
};


// findIndicies :: (a -> Bool) -> [[a]] -> [(Int, Int)]
fork icon0
star icon0
watch icon0

1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
 * @example
 *
 *      const duplicate = n => [n, n];
 *      R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3]
 *
 *      R.chain(R.append, R.head)([1, 2, 3]); //=> [1, 2, 3, 1]
 */




var chain =
fork icon0
star icon0
watch icon2

+ 15 other calls in file

6
7
8
9
10
11
12
13
14
15
16
17


const toFuture = ramda.unless(fluture.isFuture, fluture.resolve);


const getter = (l) => (data) => (ramda.compose(
    ramda.chain((value) => ramda.isNil(value) ? fluture.reject(data) : fluture.resolve(value)),
    ramda.chain((value) => toFuture(value)),
    ramda.map(ramda.view(l)),
    toFuture
)(data));

fork icon0
star icon0
watch icon0

+ 5 other calls in file

47
48
49
50
51
52
53
54
55
56
57
)(lines);


const noOverlaps = R.compose(
    R.length,
    R.filter(R.lt(1)),
    R.chain(R.values),
    R.values);


console.log(noOverlaps(oceanFloor));
fork icon0
star icon0
watch icon0

16
17
18
19
20
21
22
23
24
25
return jwt.sign(({
  productId,
  optionId,
  availabilityId: root.pk,
  currency,
  customers: R.chain(u => {
    const foundCustomerTypeRate = root.customer_type_rates.find(c => `${R.path(['customer_prototype', 'pk'], c)}` === `${u.unitId}`) || {};
    return new Array(u.quantity).fill(1).map(() => ({
      customer_type_rate: foundCustomerTypeRate.pk,
    }));
fork icon0
star icon0
watch icon0

29
30
31
32
33
34
35
36
37
38
39
40
41


const vectorComponentCap = ([x, y]) => [x !== 0 ? x / Math.abs(x) : 0, y !== 0 ? y / Math.abs(y) : 0]


const instructions = R.compose(
  R.map(directionToVector),
  R.chain(R.converge(R.times, [R.compose(R.always, R.head), R.last])),
  R.map(R.compose(R.over(lastLens, Number), R.split(' ')))
)(content)


const moveRopeKnots = R.curry((rope, direction) => R.converge(
fork icon0
star icon0
watch icon0

18
19
20
21
22
23
24
25
26
27
const newBoard = R.reduce((board, [i, j]) => {
  board[i][j] = -1;
  return board;
}, board, flashPoints);

const addList = R.chain((p) => U.getNeighbours(board, p, true), flashPoints);
addList.forEach((pos) => {
  const [s, t] = pos;
  if (board[s][t] !== -1) board[s][t] += 1;
});
fork icon0
star icon0
watch icon0

Other functions in ramda

Sorted by popularity

function icon

ramda.clone is the most popular function in ramda (30311 examples)