How to use the addIndex function from ramda

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

ramda.addIndex is a function in the Ramda library for JavaScript that applies a function to an array, passing in the index of each element as an additional argument.

113
114
115
116
117
118
119
120
121
122
123
124
125
126


const valueForKey = (v) => v[key(v)]


const returnArray = (prop) => R.pathOr([], [prop])


const mapIndexed = R.addIndex(R.map)


const set = (arr, prop) =>
  match(prop)
    .on(exists, (prop) => {
fork icon3
star icon44
watch icon6

27
28
29
30
31
32
33
34
35
36

const scopes = R.pipe(
  R.reduce(allPathCombinations, []),
  R.sort((a, b) => a.localeCompare(b)),
  R.uniq,
  R.addIndex(R.map)((name, index) => {
    return {
      value: name,
      name: `${index + 1}) ${name}`,
    }
fork icon11
star icon40
watch icon12

How does ramda.addIndex work?

The ramda.addIndex function in the Ramda library for JavaScript is used to apply a function to an array, passing in the index of each element as an additional argument. When called, ramda.addIndex takes two arguments: a function fn to be applied to the array, and the array list to be processed. The ramda.addIndex function then applies the function fn to each element of the array list, passing in the index of each element as an additional argument. The function fn is called with three arguments: the value of the element, the index of the element, and the entire array being processed. For example, you could use ramda.addIndex to add the index of each element to an array like this: javascript Copy code {{{{{{{ import { addIndex, map } from 'ramda'; const list = ['a', 'b', 'c']; const result = addIndex(map)((value, index) => `${value}-${index}`, list); console.log(result); // logs ['a-0', 'b-1', 'c-2'] In this example, we first import the addIndex and map functions from the Ramda library using the ES6 import statement. We then define an array list with three string elements. We call addIndex(map) with a function that takes two arguments: the value of the element and the index of the element. This function returns a string that combines the value of the element and its index. Finally, we pass the function and the list array to addIndex(map) to apply the function to each element of the array, passing in the index of each element as an additional argument. We store the result as result and log it to the console using console.log. In summary, ramda.addIndex is a function in the Ramda library for JavaScript that applies a function to an array, passing in the index of each element as an additional argument. The function fn is called with three arguments: the value of the element, the index of the element, and the entire array being processed.

13
14
15
16
17
18
19
20
21
22
const Lexeme = require('../model/lexeme')
const Err = require('../model/error')

const chain = func => o => o.chain(func)

const indexation = addIndex(map)(useWith(pair, [prop('value'), identity]))

class IndexMap {
  constructor(context = []) {
    const arr = indexation(context)
fork icon0
star icon10
watch icon4

+ 13 other calls in file

50
51
52
53
54
55
56
57
58
59
      R.identity
    )
  )
);

const mapIndexed = R.addIndex(R.map);

const coerceApiDescriptions = R.compose(
  mapIndexed((content, index) => ({
    location: `configuration.apiDescriptions[${index}]`,
fork icon303
star icon0
watch icon2

Ai Example

1
2
3
4
5
6
import { addIndex, map } from "ramda";

const list = ["a", "b", "c"];

const result = addIndex(map)((value, index) => `${value}-${index}`, list);
console.log(result); // logs ['a-0', 'b-1', 'c-2']

In this example, we first import the addIndex and map functions from the Ramda library using the ES6 import statement. We then define an array list with three string elements. We call addIndex(map) with a function that takes two arguments: the value of the element and the index of the element. This function returns a string that combines the value of the element and its index. Finally, we pass the function and the list array to addIndex(map) to apply the function to each element of the array, passing in the index of each element as an additional argument. We store the result as result and log it to the console using console.log. When you run this example in a JavaScript environment like a web browser or Node.js, you should see the array ['a-0', 'b-1', 'c-2'] logged to the console, indicating that the index of each element has been successfully added to the array using ramda.addIndex.

61
62
63
64
65
66
exports.cleanDirectories = R.curry(cleanDirectories);
exports.cleanNodeInput = cleanNodeInput;
exports.isStringType = isStringType;
exports.fileTypeInDir = R.curry(fileTypeInDir);
exports.lengthZero = lengthZero;
exports.mapIndexed = R.addIndex(R.map);
fork icon1
star icon0
watch icon2

137
138
139
140
141
142
143
144
145
146
147
148
149
  }


  return match.groups
})


const enumerate = R.addIndex(R.map)((x, i) => [i + 1, x])


function* iterateStraightLines([x0, y0], [x1, y1]) {
  if (x0 === x1) {
    if (y0 < y1) {
fork icon0
star icon1
watch icon0

+ 4 other calls in file

5
6
7
8
9
10
11
12
13
14
15
16
17
console.log('\n=== addIndex ===');
const double = R.multiply(2);
console.log(R.map(double, [5, 10, 15, 20, 25]));


const doublePlusIndex = (v, i) => double(v) + i;
const indexedMap = R.addIndex(R.map);


console.log(indexedMap(doublePlusIndex, [5, 10, 15, 20, 25]));


/**
fork icon0
star icon0
watch icon0

+ 2 other calls in file

2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
* @category List
* @sig (a -> *) -> [a] -> [a]
* @param {Function} fn The function to invoke. Receives one argument, `value`.
* @param {Array} list The list to iterate over.
* @return {Array} The original list.
* @see R.addIndex
* @example
*
*      var printXPlusFive = x => console.log(x + 5);
*      R.forEach(printXPlusFive, [1, 2, 3]); //=> [1, 2, 3]
fork icon0
star icon0
watch icon0

+ 89 other calls in file

23
24
25
26
27
28
29
30
31
32
//     x =>x.toUpperCase(),
//     person
//   )
// )
// console.log(
//   R.addIndex(R.map)(
//     (val,ind) => val +": "+ind,
//     [1,2,3,4,5]
//   )
// )
fork icon0
star icon0
watch icon0

+ 2 other calls in file

52
53
54
55
56
57
58
59
60
61
62
63
})


const part1 = R.compose(
  R.length,
  R.filter(R.identity),
  R.addIndex(R.map)(isVisible),
)(flatForest)


const part2 = R.compose(
  R.apply(Math.max),
fork icon0
star icon0
watch icon0

+ 5 other calls in file

326
327
328
329
330
331
332
333
334
335
336
337
338
log(R.lastIndexOf(10)([1, 2, 3, 4]));


var double = x => x * 2;
log(R.map(double)(numbers));


var mapIndexed = R.addIndex(R.map);


var logMap = (val, idx) => {
  log(idx, val);
}
fork icon0
star icon0
watch icon0

+ 9 other calls in file

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


const permutations = (tokens, subperms = [[]]) =>
  R.isEmpty(tokens) ?
    subperms        :
    R.addIndex(R.chain)((token, idx) => permutations(
      R.remove(idx, 1, tokens), 
      R.map(R.append(token), subperms)
    ), tokens);

fork icon0
star icon0
watch icon0

264
265
266
267
268
269
270
271
272
* @sig ((a ... -> b) ... -> [a] -> *) -> ((a ..., Int, [a] -> b) ... -> [a] -> *)
* @param {Function} fn A list iteration function that does not pass index or list to its callback
* @return {Function} An altered list iteration function that passes (item, index, list) to its callback
* @example
*
*      const mapIndexed = R.addIndex(R.map);
*      mapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']);
*      //=> ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']
*/
fork icon0
star icon0
watch icon2

+ 7 other calls in file

0
1
2
3
4
5
6
7
8
9
10
11
const { readFile } = require('fs');
const { promisify } = require('util');
const { splitWhenever, equals, map, sum, addIndex, sortWith, descend, prop, head } = require('ramda');


const read = promisify(readFile);
const mapWithIndex = addIndex(map);
const sortByCaloriesDesc = sortWith([descend(prop('calories'))]);


read('./input')
    .then(String)
fork icon0
star icon0
watch icon0

17
18
19
20
21
22
23
24
25
26
27
28
)(content)


const part1 = R.compose(
  R.sum,
  R.converge(Array.of, indices),
  R.addIndex(R.map)((value, index) => R.multiply(value, R.inc(index))),
)(registerValues)


const screen = R.addIndex(R.map)((_, i) => R.inc(i), Array(40*6))

fork icon0
star icon0
watch icon0

+ 3 other calls in file

22
23
24
25
26
27
28
29
30
31
    console.error(e)
  }
}

exports.generateHoldingsReport = function(investmentData, companyData) {
  const chainIndexed = R.addIndex(R.chain);
  const mapIndexed = R.addIndex(R.map);

  return chainIndexed((investment, investmentIndex) =>
  mapIndexed((holding, holdingIndex) => ({
fork icon0
star icon0
watch icon1

+ 3 other calls in file

Other functions in ramda

Sorted by popularity

function icon

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