LiMonad is a tiny library for building monads.
Here are some examples of how to build common monads with LiMonad:
Example:
MaybeMonad >>
-> x { 5 / x unless x == 0 } >>
-> x { x + 1 } >>
-> x { 3 * x } >>
-> x { Math.log(x) unless x < 0 } >>
-> x { x.round }
Monad Definition:
class MaybeMonad < LiMonad
def bind(f, g, *args)
result = f[*args]
g[result] if result
end
end
Example:
LoggerMonad >>
-> x { [x * x, "#{x} was squared"] } >>
-> x { [x / 2, "#{x} was halved"] }
Monad Definition:
class LoggerMonad < LiMonad
def bind(f, g, *args)
f_result, f_log = f[*args]
g_result, g_log = g[f_result]
[g_result, "#{f_log}\n#{g_log}"]
end
def unit(g, *args)
[g[*args], args.inspect]
end
end
Add this line to your application's Gemfile:
gem 'limonad'
And then execute:
$ bundle
Or install it yourself as:
$ gem install limonad
- Fork it ( https://github.com/nakhli/limonad/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Licensed under the MIT License.
Copyright Chaker Nakhli.