Skip to content

Instantly share code, notes, and snippets.

@vysogot
Created October 16, 2019 14:04
Show Gist options
  • Save vysogot/29b9dce8c68fffdb8246f5184fce8ee9 to your computer and use it in GitHub Desktop.
Save vysogot/29b9dce8c68fffdb8246f5184fce8ee9 to your computer and use it in GitHub Desktop.
Ruby lambda factorial
IS_ZERO = -> (x) { x == 0 }
ONE = -> { 1 }
ONE_LESS = -> (x) { x - 1 }
ITSELF = -> (x) { x }
COND = -> (cond) { -> (doit) { -> (doelse) { cond ? doit.(nil) : doelse.(nil) } } }
MULT = -> (x) { -> (y) { x * y } }
puts(
-> myself {
-> (n) {
COND.(
IS_ZERO.(n)
).(
-> _ { ONE.() }
).(
-> _ { MULT.(ITSELF.(n)).(myself.(myself).(ONE_LESS.(n))) }
)
}
}.(
-> myself {
-> (n) {
COND.(
IS_ZERO.(n)
).(
-> _ { ONE.() }
).(
-> _ { MULT.(ITSELF.(n)).(myself.(myself).(ONE_LESS.(n))) }
)
}
}
).(6)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment