Factorial

Recursive:

func factorial_recursive(n) {
    n == 0 ? 1 : (n * __FUNC__(n-1))
}

Catamorphism:

func factorial_reduce(n) {
    1..n -> reduce({|a,b| a * b }, 1)
}

Iterative:

func factorial_iterative(n) {
    var f = 1
    {|i| f *= i } << 2..n
    return f
}

Built-in:

say 5!

Last updated