How to use the aperture function from ramda

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

ramda.aperture is a function in the Ramda library that returns a new list by applying a sliding window to a given list.

1
2
3
4
5
6
7
8
9
10
11
const { parseText } = require('../utils')


const makeMarkerFinder = (length) =>
  R.pipe(
    parseText,
    R.aperture(length),
    R.findIndex(R.pipe(R.countBy(R.identity), R.values, R.all(R.equals(1)))),
    R.add(length),
  )

fork icon0
star icon1
watch icon0

+ 2 other calls in file

16
17
18
19
20
21
22
23
24
25
26
  ),
)


const boardFromInput = R.converge(
  R.reduce((acc, row) => {
    for (const [from, to] of R.aperture(2, row)) {
      for (const pos of iterateStraightLines(from, to)) {
        acc[vec.key(pos)] = '#'
      }
    }
fork icon0
star icon1
watch icon0

How does ramda.aperture work?

ramda.aperture is a function in the Ramda library that creates a new array of subarrays of a given length, with each subarray being created by applying a sliding window to the original array. The aperture function takes two arguments: the first argument is the length of each subarray, and the second argument is the original array to be split. The resulting array will contain n - m + 1 subarrays, where n is the length of the original array and m is the length of the subarrays. Each subarray is created by taking a slice of the original array starting at index i and ending at index i + m - 1, where i is the index of the first element in the subarray.

67
68
69
70
71
72
73
74
75

const done = () => {
  context.parts.push(undefined)
  const recordNumber = context.recordNumber
  const shapeType = context.shapeType
  const points = R.aperture(2, context.parts).map(([start, end]) => context.points.slice(start, end))
  next(factory({ recordNumber, shapeType, box: context.box, points }))
  return (decoders => decoders.pop())
}
fork icon0
star icon0
watch icon1

+ 3 other calls in file

1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
 * @param {Array} list The list to split into `n`-tuples
 * @return {Array} The new list.
 * @see R.transduce
 * @example
 *
 *      R.aperture(2, [1, 2, 3, 4, 5]); //=> [[1, 2], [2, 3], [3, 4], [4, 5]]
 *      R.aperture(3, [1, 2, 3, 4, 5]); //=> [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
 *      R.aperture(7, [1, 2, 3, 4, 5]); //=> []
 */
var aperture = _curry2(_dispatchable('aperture', _xaperture, _aperture));
fork icon0
star icon0
watch icon0

+ 53 other calls in file

Ai Example

1
2
3
4
const R = require("ramda");

const pairs = R.aperture(2, [1, 2, 3, 4, 5]);
console.log(pairs); // output: [ [1, 2], [2, 3], [3, 4], [4, 5] ]

In the above example, ramda.aperture is used to create pairs of adjacent elements in an array of numbers. The function takes two arguments: the size of each group and the array to process. In this case, aperture(2, [1, 2, 3, 4, 5]) creates pairs of two adjacent elements in the array, resulting in [[1, 2], [2, 3], [3, 4], [4, 5]].

64
65
66
67
68
69
70
//     ),
//     R.prop("howManyCats")
//   )(cats)
// );
console.log(
  R.aperture(1, [1,2,3,4])
)
fork icon0
star icon0
watch icon0

+ 2 other calls in file

4
5
6
7
8
9
10
11
12
13
14
  R.sort((a, b) => a - b),
  (arr) => [0, ...arr, R.last(arr) + 3]
)(data)


const countJoltageDiffs = R.pipe(
  R.aperture(2),
  R.map(([a, b]) => b - a),
  R.countBy(R.identity),
  (counts) => counts["1"] * counts["3"]
)
fork icon0
star icon0
watch icon0

5
6
7
8
9
10
11
12
13
14
15
const dataPath = path.join(__dirname, process.argv[2])
const data = inputTools.loadInputFile(dataPath)


function detectMarker(data) {
  /*
  const words = R.aperture(4, data)
  const uniqueCounts = R.compose(
    R.add(4),
    R.findIndex(R.equals(4)),
    R.map(R.compose(R.length, R.uniq)),
fork icon0
star icon0
watch icon0

49
50
51
52
53
54
55
56
57
58
59
    R.compose(
      lineComponents,
      R.map(R.last)
    )
  ])),
  R.aperture(2)
)


const walledCave = R.compose(
  R.reduce((acc, index) => R.set(R.lensIndex(index), '#', acc), cave),
fork icon0
star icon0
watch icon0

+ 2 other calls in file

Other functions in ramda

Sorted by popularity

function icon

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