Last active
May 6, 2017 09:56
-
-
Save masahirompp/a1b2fd217a34d639669c17c9e0fe3b5a to your computer and use it in GitHub Desktop.
配列のすべての組み合わせについて、関数を実行する。
This file contains hidden or 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
import flatten from 'lodash-es/flatten' | |
/** | |
* 配列のすべての組み合わせについて、関数を実行する。 | |
* @param fun 関数 | |
* @param cols 配列 | |
* @return {Array} | |
* @example | |
* combination((x,y)=>x*y, [1,2,3], [4,5,6]) | |
* => [4,5,6,8,10,12,12,15,18] | |
* | |
* combination((x,y)=>x*y, [1,2,3], [1,2,4], [1,2]) | |
* => combination((x,y)=>x*y, [1,2,4,2,4,8,3,6,12], [1,2]) | |
* => [1,2,2,4,4,8,2,4,4,8,8,16,3,6,6,12,12,24] | |
*/ | |
export function combination (fun: Function, ...cols: any[][]) { | |
return cols.reduce((col1, col2) => { | |
return flatten(col1.map((c1: any) => col2.map((c2: any) => fun(c1, c2)))) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment