Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Functional enhancements for ruby.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 README.md
Octocat-spinner-32 functional.rb
README.md

Functional enhancements for ruby.

Some utils taken from the book "The Ruby Programming Language 6.8 Functional Programming"

Function composition.

>> sqr = lambda {|x| x * x }
>> inc = lambda {|x| x + 1 }
>> (sqr * inc)[5]
=> 36

Function piping.

>> sqr = lambda {|x| x * x }
>> inc = lambda {|x| x + 1 }
>> (sqr | inc)[5]
=> 26

Partial function - first arguments.

>> pow = lambda {|x, y| x ** y }
>> (pow >> 10)[2]
=> 100

Partial function - last arguments.

>> pow = lambda {|x, y| x ** y }
>> (pow << 10)[2]
=> 1024

Memoize.

>> 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 

Working with methods.

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
Something went wrong with that request. Please try again.