bossiernesto / functional_rb

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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)

About


Languages

Language:Ruby 100.0%