Skip to content

Instantly share code, notes, and snippets.

@bas080
Created February 16, 2021 08:59
Show Gist options
  • Save bas080/6263d246e3429681a59fc2550a73fe8d to your computer and use it in GitHub Desktop.
Save bas080/6263d246e3429681a59fc2550a73fe8d to your computer and use it in GitHub Desktop.
function scan(fn, list, result = []) {
if (list.length < 2)
return result
const [a, b, ...rest] = list
return scan(fn, [b, ...rest], [...result, fn(a, b)])
}
function* pascal(row = [1]) {
yield row
if (row.length === 1)
yield row = [...row, ...row]
while (true) {
yield row = [row[0], ...scan(add, row), row[0]]
}
}
const add = (a, b) => a + b
const gen = pascal([7])
console.log(gen.next())
console.log(gen.next())
console.log(gen.next())
console.log(gen.next())
console.log(gen.next())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment