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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
odoo.define('web.mathUtils', function () {
"use strict";
/**
* Same values returned as those returned by cartesian function for case n = 0
* and n > 1. For n = 1, brackets are put around the unique parameter elements.
*
* @returns {Array}
*/
function _cartesian() {
var args = Array.prototype.slice.call(arguments);
if (args.length === 0) {
return [undefined];
}
var firstArray = args[0].map(function (elem) {
return [elem];
});
if (args.length === 1) {
return firstArray;
}
var productOfOtherArrays = _cartesian.apply(null, args.slice(1));
var result = firstArray.reduce(
function (acc, elem) {
return acc.concat(productOfOtherArrays.map(function (tuple) {
return elem.concat(tuple);
}));
},
[]
);
return result;
}
/**
* Returns the product of any number n of arrays.
* The internal structures of their elements is preserved.
* For n = 1, no brackets are put around the unique parameter elements
* For n = 0, [undefined] is returned since it is the unit
* of the cartesian product (up to isomorphism).
*
* @returns {Array}
*/
function cartesian() {
var args = Array.prototype.slice.call(arguments);
if (args.length === 0) {
return [undefined];
} else if (args.length === 1) {
return args[0];
} else {
return _cartesian.apply(null, args);
}
}
/**
* Returns all initial sections of a given array, e.g. for [1, 2] the array
* [[], [1], [1, 2]] is returned.
*
* @param {Array} array
* @returns {Array[]}
*/
function sections(array) {
var sections = [];
for (var i = 0; i < array.length + 1; i++) {
sections.push(array.slice(0, i));
}
return sections;
}
return {
cartesian: cartesian,
sections: sections,
};
});
|