Skip to content

Instantly share code, notes, and snippets.

@rxw1
Last active December 30, 2015 18:09
Show Gist options
  • Save rxw1/7865583 to your computer and use it in GitHub Desktop.
Save rxw1/7865583 to your computer and use it in GitHub Desktop.
Multiply arrays in an array
multiply: (a, n) ->
i = 0
while i < a.length
j = 0
while j < n - 1
a[i] = a[i].concat(a[i])
j++
i++
a
var a = [
["a", "b", "c"],
["d", "e"],
["f"]
];
//multiply (clone?) arrays of an array
var ma = function(a, n) {
for (var i = 0; i < a.length; i++) {
for (var j = 0; j < n - 1; j++) {
a[i] = a[i].concat(a[i]);
}
}
return a;
}
/*
same same, but separate functions */
*/
// multiply an array
var m = function(a, n) {
for (var i = 0; i < n - 1; i++) {
a = a.concat(a);
}
return a;
};
// do something with each array in an array n times
var ma = function(a, n) {
for (var i = 0; i < a.length; i++) {
a[i] = m(a[i], n);
}
return a;
}
console.log(ma(a, 2));
n=2; [("a".."c").to_a, ("d".."g").to_a, ("h".."i").to_a, ["j"]].map{|x| x*n}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment