Ribimaybe

"Flavouring the ocean with a teaspoon of sugar."

Gem Version Travis Code Climate

A tiny Ruby library that provides a Maybe datatype which is a Functor, Applicative Functor and Monad.

Installation

Add this line to your application's Gemfile:

gem 'ribimaybe'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ribimaybe

Usage

This is a small library and so it doesn't offer lots of creature comforts. The one escape hatch it does offer is the ability to convert nil into Nothing.

include Ribimaybe::Maybe

Maybe(42)  # => Just(42)
Maybe(nil) # => Nothing

And that's it, once you have lifted your value into a Maybe you can treat it as a Functor, Applicative Functor or Monad. If you want to pull your value out of a Maybe, we got you covered too.

Just(42).maybe(false) { |x| x == 42 } # => true
Nothing.maybe(false)  { |x| x == 42 } # => false 

Functor [info]

include Ribimaybe::Maybe

# Apply functions within Maybe and retain structure.
Just(42).map { |x| x * x } # => Just(1764)
Nothing.map  { |x| x * x } # => Nothing

Applicative Functor [info]

include Ribimaybe::Maybe

# Wrap functions inside functors and apply them to other functors! 
Just do |x, y|
  x * y
end.apply(pure(42)).apply(pure(42)) # => Just(1764)

Just do |x|
  x * x
end.apply(Nothing) # => Nothing

Monad [info]

include Ribimaybe::Maybe

# Chain together computations and pretend you're a Haskeller.
Just(42).bind do |x|
  rturn(x - 21)
end.bind do |x|
  rturn(x * 2)
end # => Just(42)

# You guessed it! If have Nothing, you get Nothing.
Nothing.bind do |x|
  rturn(x * x)
end # => Nothing

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request