User loginNavigation |
Function Readability & UnderstandabilityTwo versions of the same function in JavaScript because its a language that supports both the functional and imperative style. The function accepts any number of arrays as arguments, and outputs the permutations of the values so (['a', 'b'],['1', '2']) becomes [['a', '1'], ['a', '2'], ['b', '1'], ['b', '2']]: function permutations1(x) { return x.reduce(function(a0, v0) { return a0.reduce(function(a1, v1) { return a1.concat(v0.map(function(v2) { return v1.concat([v2]); })); }, []); }, [[]]); } function permutations2(x) { var a = [[]]; for (var i = 0; i < x.length; ++i) { var b = []; for (var j = 0; j < a.length; ++j) { for (var k = 0; k < x[i].length; ++k) { b.push(a[j].concat([x[i][k]])); } } a = b; } return a; } Which do you find easier to read and understand, and if you find one easier, what do you think the reasons are? By Keean Schupke at 2014-12-19 13:23 | LtU Forum | previous forum topic | next forum topic | other blogs | 9824 reads
|
Browse archives
Active forum topics |
Recent comments
20 weeks 1 day ago
20 weeks 1 day ago
20 weeks 1 day ago
42 weeks 2 days ago
46 weeks 4 days ago
48 weeks 1 day ago
48 weeks 1 day ago
50 weeks 6 days ago
1 year 3 weeks ago
1 year 3 weeks ago