Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi array calc #43

Merged
merged 4 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,22 @@ function arrayCalc(a=0, v=0, func=()=>{return a;}){
}
exports.arrayCalc = arrayCalc;

// Call a list function with provided arguments
// The difference is that first all the possible combinations of the arrays
// are calculated allowing arrays as arguments to generate
// multiple versions of the function and joining them together
//
function multiCall(func, ...a){
// calculate the array combinations
let args = arrayCombinations(...a);
// call the function for all the argument combinations
args = args.map((a) => func(...a));
// combine into a single list but preserving multi-dimensional arrays
let out = flatten(args, 1);
return out;
}
exports.multiCall = multiCall;

// Alternate through 2 or multiple lists consecutively
// The output length is the lowest common denominator of the input lists
// so that every combination of consecutive values is included
Expand All @@ -306,16 +322,19 @@ exports.arrayCalc = arrayCalc;
// @return {Array} -> outputs a 2D array of the results
//
function arrayCombinations(...arrs){
// make sure all values are array
// make sure all items are an array of at least 1 item
arrs = arrs.map(a => toArray(a));
// the output is the unique list sizes multiplied
// get the lengths, but remove duplicate lengths
let sizes = unique(arrs.map(a => a.length));
// multiply to get total of possible iterations
let iters = 1;
sizes.forEach((l) => iters *= l);
// iterate over the total amount pushing the items to array
let arr = [];
for (let i=0; i<iters; i++){
arr.push(arrs.map((e) => e[i % e.length] ));
arr.push(arrs.map((e) => {
return e[i % e.length]
}));
}
return arr;
}
Expand Down
44 changes: 44 additions & 0 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1416,10 +1416,54 @@ function testUtility(){
test("Util.arrayCombinations([1, 2, 3], [10, 20])", () => {
expect(Util.arrayCombinations([1, 2, 3], [10, 20])).toStrictEqual([[1, 10], [2, 20], [3, 10], [1, 20], [2, 10], [3, 20]]);
});
test("Util.arrayCombinations([1, 2, 3, 4], [10, 20])", () => {
expect(Util.arrayCombinations([1, 2, 3, 4], [10, 20])).toStrictEqual([[1, 10], [2, 20], [3, 10], [4, 20], [1, 10], [2, 20], [3, 10], [4, 20]]);
});
test("Util.arrayCombinations([1, [2, 3]], [10, 20], [100, 200])", () => {
expect(Util.arrayCombinations([1, [2, 3]], [10, 20], [100, 200])).toStrictEqual([[1, 10, 100], [[2, 3], 20, 200]]);
});

test("Util.multiCall(Gen.spreadInc, 5, [12, 24])", () => {
expect(Util.multiCall(Gen.spreadInc, 5, [12, 24])).toStrictEqual([0, 3, 6, 9, 12, 0, 6, 12, 18, 24]);
});
test("Util.multiCall(Algo.fastEuclid, 8, [3, 5])", () => {
expect(Util.multiCall(Algo.fastEuclid, 8, [3, 5])).toStrictEqual([1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1]);
});
test("Util.multiCall(Mod.rotate, [Algo.hex('ed6')], [0, -1])", () => {
expect(Util.multiCall(Mod.rotate, [Algo.hex('ed6')], [0, -1])).toStrictEqual([1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1]);
});
test("Util.multiCall(Util.add, [[0, 3, 7, 12]], [0, 12])", () => {
expect(Util.multiCall(Util.add, [[ 0, 3, 7, 12 ]], [0, 12])).toStrictEqual([0, 3, 7, 12, 12, 15, 19, 24]);
});
test("Util.multiCall(Util.add, [0, 3, 7, 12], [0, 12])", () => {
expect(Util.multiCall(Util.add, [0, 3, 7, 12], [0, 12])).toStrictEqual([0, 15, 7, 24, 0, 15, 7, 24]);
});

// let amounts = 8;
// let hits = [3, 5, 7];
// // let highs = [12, 24];
// console.log(Util.multiCall(Gen.spreadInc, 5, [12, 24, 36]));
// console.log(Util.multiCall(Algo.fastEuclid, 8, [3, 5, 7]));

// console.log(Util.multiCall(Mod.rotate, [Algo.hex('ed6')], [0, -1, -2]));

// console.log(Util.arrayCombinations([0, 3, 7, 12], [0, 12]));
// console.log(Util.multiCall(Util.add, [[0, 3, 7, 12]], [0, 12, 24]));

// let times = Util.arrayCombinations(amounts, hits);
// let results = Util.flatten(times.map((t) => Gen.spread(...t) ), 1);
// let results = Util.flatten(times.map((t) => Algo.fastEuclid(...t) ), 1);
// let arr = Gen.spread(10, 3, 12);
// console.log(times);
// console.log(results);

// let someList = [[10, 20], [[30, 300], 40], [50, [600, 60] ]];
// let someList = [[10, 20], [30, 300, 40], [50, 600, 60 ]];
// console.log(someList)
// console.log(Util.flat(someList, 1))

// Util.arrayCombinations([])

test("Util.map()", () => {
expect(Util.map()).toStrictEqual(0);
});
Expand Down