Skip to content

Instantly share code, notes, and snippets.

@xgrommx
Created October 16, 2015 15:06
Show Gist options
  • Save xgrommx/eaeba02199c6d3c52a98 to your computer and use it in GitHub Desktop.
Save xgrommx/eaeba02199c6d3c52a98 to your computer and use it in GitHub Desktop.
ES6 Combinators
// λh.(h h)
let U = f => f(f)
// (λ x. x x) (λ x. x x)
let Ω = _ => (x => x (x)) (x => x (x))
// λf.(λx.f (λv.((x x) v))) (λx.f (λv.((x x) v)))
let Z = f => (x => f(y => x(x)(y)))
(x => f(y => x(x)(y)))
let fact = Z(f =>
n => n == 0 ? 1 : n * f(n - 1)
)
let fact = U(f =>
n => n == 0 ? 1 : n * (f(f)(n - 1))
)
var fix = f => (x => f(fix(f))(x));
var factabs = fact => (x => x == 0 ? 1 : x * fact(x - 1));
var a = fix(factabs)(5); /* 120 */
let fix = f => (x => f(fix(f))(x))
let New = fix
let Animal = body => body
let Base = Self => Animal({
speak: repeats => "",
move: repeats => "",
act: repeats => Self.move(repeats) + Self.speak(repeats)
})
let Bird = Self => (Super => Animal({
speak: repeats => Array(repeats).join("cheep "),
move: repeats => Array(repeats).join("flap "),
act: Super.act
}))(Base(Self))
let Dog = Self => (Super => Animal({
speak: repeats => Array(repeats).join("bark "),
move: repeats => Array(repeats).join("run "),
act: Super.act
}))(Base(Self))
let b = New(Bird)
let d = New(Dog)
b.act(2) //b.act is not a function?
d.act(2) // d.act is not a function?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment