A maybe monad
This release is the first 1.x release, which marks that the project is now officially production ready. The gem is battle tested in real life. We at Sharetribe have been using the gem in our production application since the first version without any issues. In addition to that, the public API of the gem hasn't seen any radical breaking changes for long time, so it's now great time to release the first 1.x version.
Added or_raise
or_raise
returns an unwrapped value for Some
and throws an exception for None
. The exception message helps you to spot where the object became None
.
users = Maybe({users: [{name: {first: "John", last: "Doe"}}]})
users[:users][0][:name][:first].or_raise
# => "John"
users[:users][1][:name][:first].or_raise
# => None::ValueExpectedException: `or_raise` called to None. A value was expected.
# => Maybe => Some({:users=>[{:name=>{:first=>"John", :last=>"Doe"}}]})
# => [:users] => Some([{:name=>{:first=>"John", :last=>"Doe"}}])
# => [1] => None
# => [:name] => None
# => [:first] => None
Added or_nil
or_nil
returns an unwrapped value for Some
and nil
for None
. It's equivalent to .or_else(nil)
users = Maybe({users: [{name: {first: "John", last: "Doe"}}]})
users[:users][0][:name][:first].or_nil # => "John"
users[:users][1][:name][:first].or_nil # => nil
Adds threequals ===
operator. This allows Maybe to be used in case expressions. See README for more information.
This release also moves Some and None classes to top-level. They are not anymore wrapped in Maybe module (Maybe::Some, Maybe::None). Also Maybe is now a top-level class, not a module. This change pollutes the global namespace a bit more, but it is required for the case expressions where it is nicer to compare to Some
than to Maybe::Some
.
When updating, there shouldn't be any breaking changes if you haven't referenced to Maybe::Some
or Maybe::None
directly or used None
constant.
none?
-> is_none?
. none?
is an Enumerable method, so it clashed with Maybe#none?
some?
-> is_some?
.eql?
and ==
to_ary
and to_a
flat_map