Skip to content

Instantly share code, notes, and snippets.

View odf's full-sized avatar

Olaf Delgado-Friedrichs odf

View GitHub Profile
@odf
odf / factorial.coffee
Created August 23, 2011 00:06
A tail-recursive factorial in Coffeescript with simulated tail-call optimization.
bounce = (val) -> val = val() while typeof val == 'function'; val
factorial = (n) ->
fact = (a, n) -> if n > 0 then -> fact a * n, n - 1 else a
bounce fact 1, n
console.log factorial 10
@odf
odf / cantor.js
Last active November 20, 2019 09:01
Flattening an infinite list of infinite lists in JavaScript via Cantor diagonalisation
class Seq {
toArray() {
const a = [];
for (const x of this)
a.push(x);
return a;
}
toString() {
return this.toArray().join(' -> ');