Created
May 13, 2018 18:35
-
-
Save ancms2600/dddcc1bf1d1a5f4e7ec9aa30ad4393a5 to your computer and use it in GitHub Desktop.
CROSS JOIN implementation in Javascript
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 crossJoin = (...args) => { | |
const m = args.reduce((acc,a)=>{ | |
acc.push(0 === acc.length ? | |
a.length : | |
(acc[acc.length-1] * a.length)); | |
return acc; | |
},[]); | |
const o = []; | |
for (const [i,letters] of args.entries()) { | |
const slots = m[m.length-1]; | |
const cmult = m[i]; | |
const letterlen = letters.length; | |
const loccur = slots / cmult; | |
const poccur = slots / (letterlen * loccur); | |
for (let poi = 0; poi<poccur; poi++) { | |
for (const [li, letter] of letters.entries()) { | |
for (let loi = 0; loi<loccur; loi++) { | |
const ipr = slots / poccur; | |
const y = ((poi * ipr)) + (li * loccur) + loi, x = i; | |
if (null == o[y]) o[y] = []; | |
o[y][x] = letter; | |
} | |
} | |
} | |
} | |
return o; | |
}; | |
crossJoin([1,2], [3,4], [5,6], [7]); // 4 x 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
notes: