summaryrefslogtreecommitdiff
path: root/jstests/libs/fun.js
blob: 276f32a8f40c8b6b71adf5c389c0fcc1e4301cde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// General high-order functions

function forEach (action, array) {
  for (var i = 0; i < array.length; i++)
    action (array[i]);
}

function foldl (combine, base, array) {
  for (var i = 0; i < array.length; i++)
    base = combine (base, array[i]);
  return base
}

function foldr (combine, base, array) {
  for (var i = array.length - 1; i >= 0; i--)
    base = combine (array[i], base);
  return base
}

function map (func, array) {
  var result = [];
  for (var i = 0; i < array.length; i++)
    result.push (func (array[i]));
  return result
}

function filter (pred, array) {
  var result = []
  for (var i = 0; i < array.length; i++)
    if (pred (array[i])) result.push (array[i]);
  return result
}