Skip to content

Instantly share code, notes, and snippets.

@tim-salabim
Last active September 2, 2020 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tim-salabim/1354991e4b8fa6c61085121e54371a1a to your computer and use it in GitHub Desktop.
Save tim-salabim/1354991e4b8fa6c61085121e54371a1a to your computer and use it in GitHub Desktop.
combiner = function(x, y = x) {
out = vector("list"); n = 1
for (i in x) {
for (j in y) {
# if (i %in% j) {
# next
# }
out[[n]] = c(i, j)
n = n + 1
}
}
return(out)
}
wrapper = function(x = list(c(0, 255), c(0, 255)), n = length(x)) {
for (h in (n - 1):1) {
out = if (h == n - 1) {
combiner(x[[h]], x[[h + 1]])
} else {
combiner(x[[h]], out)
}
}
return(out)
}
combineArrs = function(x, y) {
out = vector("list", length(x))
for (i in 1:length(x)) {
out[[i]] = c(x[i], y[i])
}
return(out)
}
a = 1:3
b = 10:12
wrapper(combineArrs(a, b))
[[1]]
[1] 1 2 3
[[2]]
[1] 1 2 12
[[3]]
[1] 1 11 3
[[4]]
[1] 1 11 12
[[5]]
[1] 10 2 3
[[6]]
[1] 10 2 12
[[7]]
[1] 10 11 3
[[8]]
[1] 10 11 12
## Thanks to Flo Detsch!!
@tim-salabim
Copy link
Author

tim-salabim commented Sep 1, 2020

In JavaScript

function combineArrs(x, y) {
  let out = [];
  for (let i = 0; i < x.length; i++) {
    out.push([x[i], y[i]]);
  }
  return out;
}

function combiner(x, y) {
    let out = new Array(x.length);
    let n = 0;
    for (let i = 0; i < x.length; i++) {
        for (let j = 0; j < y.length; j++) {
            out[n] = [x[i], y[j]];
            n = n + 1;
        }
    }
    return out;
}

function wrapper(x, len) {
    let out = [];
    for (let h = len - 2; h >= 0; h--) {
        if (h === len - 2) {
            out = combiner(x[h], x[h + 1]);
        } else {
            out = combiner(x[h], out);
        }
        for (let k = 0; k < out.length; k++) {
            out[k] = out[k].flat();
        }
    }
    return out;
}

var a = [1, 2, 3];
var b = [10, 11, 12];

wrapper(combineArrs(a, b), a.length)
0: (3) [1, 2, 3]
1: (3) [1, 2, 12]
2: (3) [1, 11, 3]
3: (3) [1, 11, 12]
4: (3) [10, 2, 3]
5: (3) [10, 2, 12]
6: (3) [10, 11, 3]
7: (3) [10, 11, 12]

@tim-salabim
Copy link
Author

tim-salabim commented Sep 2, 2020

To get rid of eval and include all Math functions:

var x = -2
undefined
var arith = "abs(x)"
undefined
function evalDomain(a) {
    return Function( 'with(Math) return ' + a)();
}
undefined
evalDomain(arith)
2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment