Last active
December 30, 2015 18:09
-
-
Save rxw1/7865583 to your computer and use it in GitHub Desktop.
Multiply arrays in an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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