Arrays¶
-
map(fn, arr)¶ Returns an array obtained by mapping the function
fnover arrayarr.map(function(x) { return x + 1; }, [0, 1, 2]); // => [1, 2, 3]
-
mapData({data: arr[, batchSize: n]}, fn)¶ Returns an array obtained by mapping the function
fnover arrayarr. Each application offnhas an element ofarras its first argument and the index of that element as its second argument.mapandmapDatadiffer in that the use ofmapDataasserts to the inference back end that all executions offnare conditionally independent. This information can potentially be exploited on a per algorithm basis to improve the efficiency of inference.mapDataalso provides an interface through which inference algorithms can support data sub-sampling. Where supported, the size of a “mini-batch” can be specified using thebatchSizeoption. When using data sub-sampling the array normally returned bymapDatais not computed in its entirety, soundefinedis returned in its place.Only the ELBO optimization objective takes advantage of
mapDataat this time.mapData({data: [0, 1, 2]}, function(x) { return x + 1; }); // => [1, 2, 3] mapData({data: data, batchSize: 10}, fn);
-
map2(fn, arr1, arr2)¶ Returns an array obtained by mapping the function
fnover arraysarr1andarr2concurrently. Each application offnhas an element ofarr1as its first argument and the element with the same index inarr2as its second argument.It is assumed that
arr1andarr2are arrays of the same length. When this is not the case the behavior ofmap2is undefined.var concat = function(x, y) { return x + y; }; map2(concat, ['a', 'b'], ['1', '2']); // => ['a1', 'b2']
-
mapN(fn, n)¶ Returns an array obtained by mapping the function
fnover the integers[0,1,...,n-1].var inc = function(x) { return x + 1; }; mapN(inc, 3); // => [1, 2, 3]
-
mapIndexed(fn, arr)¶ Returns the array obtained by mapping the function
fnover arrayarr. Each application offnhas the index of the current element as its first argument and the element itself as its second argument.var pair = function(x, y) { return [x, y]; }; mapIndexed(pair, ['a', 'b']); // => [[0, 'a'], [1, 'b']]
-
reduce(fn, init, arr)¶ Reduces array
arrto a single value by applying functionfnto an accumulator and each value of the array.initis the initial value of the accumulator.reduce(function(x, acc) { return x + acc; }, 0, [1, 2, 3]); // => 6
-
sum(arr)¶ Computes the sum of the elements of array
arr.It is assumed that each element of
arris a number.sum([1, 2, 3, 4]) // => 10
-
product(arr)¶ Computes the product of the elements of array
arr.It is assumed that each element of
arris a number.product([1, 2, 3, 4]) // => 24
-
listMean(arr)¶ Computes the mean of the elements of array
arr.It is assumed that
arris not empty, and that each element is a number.listMean([1, 2, 3]); // => 2
-
listVar(arr[, mean])¶ Computes the variance of the elements of array
arr.The
meanargument is optional. When supplied it is expected to be the mean ofarrand is used to avoid recomputing the mean internally.It is assumed that
arris not empty, and that each element is a number.listVar([1, 2, 3]); // => 0.6666...
-
listStdev(arr[, mean])¶ Computes the standard deviation of the elements of array
arr.The
meanargument is optional. When supplied it is expected to be the mean ofarrand is used to avoid recomputing the mean internally.It is assumed that
arris not empty, and that each element is a number.listStdev([1, 2, 3]); // => 0.8164...
-
all(predicate, arr)¶ Returns
truewhen all of the elements of arrayarrsatisfypredicate, andfalseotherwise.all(function(x) { return x > 1; }, [1, 2, 3]) // => false
-
any(predicate, arr)¶ Returns
truewhen any of the elements of arrayarrsatisfypredicate, andfalseotherwise.any(function(x) { return x > 1; }, [1, 2, 3]) // => true
-
zip(arr1, arr2)¶ Combines two arrays into an array of pairs. Each pair is represented as an array of length two.
It is assumed that
arr1andarr2are arrays of the same length. When this is not the case the behavior ofzipis undefined.zip(['a', 'b'], [1, 2]); // => [['a', 1], ['b', 2]]
-
filter(predicate, arr)¶ Returns a new array containing only those elements of array
arrthat satisfypredicate.filter(function(x) { return x > 1; }, [0, 1, 2, 3]); // => [2, 3]
-
find(predicate, arr)¶ Returns the first element of array
arrthat satisfiespredicate. When no such element existsundefinedis returned.find(function(x) { return x > 1; }, [0, 1, 2]); // => 2
-
remove(element, arr)¶ Returns a new array obtained by filtering out of array
arrelements not equal toelement.remove(0, [0, -1, 0, 2, 1]); // => [-1, 2, 1]
-
groupBy(eqv, arr)¶ Splits an array into sub-arrays based on pairwise equality checks performed by the function
eqv.var sameLength = function(x, y) { return x.length === y.length; }; groupBy(sameLength, ['a', 'ab', '', 'bc']); // => [['a'], ['ab', 'bc'], ['']]
-
repeat(n, fn)¶ Returns an array of length
nwhere each element is the result of applyingfnto zero arguments.repeat(3, function() { return true; }); // => [true, true, true]
-
sort(arr[, predicate[, fn]])¶ Returns a sorted array.
Elements are compared using
<by default. This is equivalent to passingltas thepredicateargument. To sort by>passgtas thepredicateargument.To sort based on comparisons between a function of each element, pass a function as the
fnargument.sort([3,2,4,1]); // => [1, 2, 3, 4] sort([3,2,4,1], gt); // => [4, 3, 2, 1] var length = function(x) { return x.length; }; sort(['a', 'ab', ''], lt, length); // => ['', 'a', 'ab']
-
sortOn(arr[, fn[, predicate]])¶ This implements the same function as
sortbut with the order of thepredicateandfnparameters switched. This is convenient when you wish to specifyfnwithout specifyingpredicate.var length = function(x) { return x.length; }; sortOn(['a', 'ab', ''], length); // => ['', 'a', 'ab']