Functional Ruby

Ruby

Another repo for showing some functional properties for the Ruby language

Features

Taken from Functors, Applicatives, And Monads In Pictures from Haskell

Maybe

Understands functor map function

just = Maybe 2
Just(42).map { |x| x * x } 
Just(1764)
Nothing.map {|x| x * x}
Nothing

Also bind

maybe_m >>= proc {|i| i + 4}
5

And applicatives

Just do |x, y|
      x ** y
    end.apply(pure(421)).apply(pure(2))
Just(177241)
proc {|x,y| x*y} % Just(5) & Just(3) 
Just(15)

Even lifting

pure(5).lift_a2 proc {|x,y| x*y}, pure(3)
Just(15)