Skip to content

Instantly share code, notes, and snippets.

@vysogot
Created October 16, 2019 14:04
Show Gist options
  • Select an option

  • Save vysogot/29b9dce8c68fffdb8246f5184fce8ee9 to your computer and use it in GitHub Desktop.

Select an option

Save vysogot/29b9dce8c68fffdb8246f5184fce8ee9 to your computer and use it in GitHub Desktop.

Revisions

  1. vysogot created this gist Oct 16, 2019.
    32 changes: 32 additions & 0 deletions ruby-lambda-factorial.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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)
    )