Some utils taken from the book "The Ruby Programming Language 6.8 Functional Programming"
>> sqr = lambda {|x| x * x }
>> inc = lambda {|x| x + 1 }
>> (sqr * inc)[5]
=> 36
>> sqr = lambda {|x| x * x }
>> inc = lambda {|x| x + 1 }
>> (sqr | inc)[5]
=> 26
>> pow = lambda {|x, y| x ** y }
>> (pow >> 10)[2]
=> 100
>> pow = lambda {|x, y| x ** y }
>> (pow << 10)[2]
=> 1024
>> fact = +lambda {|n| return 1 if n <= 1; n * fact[n-1]}
>> fact[10]
=> 3628800
>> fib = +lambda {|n| return n if n <= 1; fib[n-1] + fib[n-2] }
>> fib[100]
=> 354224848179261915075
I couldn't think of anything else other than patching the Symbol
module and overloading +@
.
>> def sqr(x)
>> x * x
>> end
>> def inc(x);
>> x + 1
>> end
>> (+:sqr | +:inc)[5]
=> 26
You can mix methods and lambdas.
>> negate = lambda {|x| -x }
>> (+:sqr | +:inc | negate)[5]
=> -26