Created
October 31, 2017 02:39
-
-
Save bas080/3d61b98ea9f3ca7736343b09239fc438 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
const side = 1; | |
/** | |
* Returns the next row based on the row that is passed in. | |
*/ | |
function row(input = []) { | |
console.log(input); | |
function recur([first, second, ...rest], next = []) { | |
if (!second) { | |
return next; | |
} | |
return recur([second, ...rest], [...next, first + second]); | |
} | |
return [side, ...recur(input), side]; | |
} | |
const result = repeat(row, 8, [side]); | |
/** | |
* Utility for repeating a function n times. The output of the first function | |
* is the input of the second and so on. | |
* | |
* Returns an array containing the return values of the function calls | |
*/ | |
function repeat(fn, times, initial) { | |
function recur(value, index = 0, result = []) { | |
if (index >= times) { | |
return result; | |
} | |
value = fn(value, index, result); | |
result.push(value); | |
return recur(value, index + 1, result); | |
} | |
return recur(initial); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment