Skip to content

Commit

Permalink
Merge pull request #29 from alex-lairan/add_or_block
Browse files Browse the repository at this point in the history
Add or block
  • Loading branch information
moba1 authored May 5, 2019
2 parents a26f01a + 9170f16 commit 0dec86e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions spec/monads/maybe/just_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ describe Monads::Just do
exclude = Monads::Just.new(3)
monad.or(exclude).should eq(monad)
end

it "result himself with lambda" do
monad = Monads::Just.new(1)
exclude = Monads::Just.new(3)
monad.or(-> { exclude }).should eq(monad)
end
end

describe "#bind" do
Expand Down
8 changes: 7 additions & 1 deletion spec/monads/maybe/nothing_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ describe Monads::Nothing do
end

describe "#or" do
it "result himself" do
it "result exclude" do
monad = Monads::Nothing(String).new
exclude = Monads::Just.new("Foo")
monad.or(exclude).should eq(exclude)
end

it "result exclude result" do
monad = Monads::Nothing(Int32).new
exclude = Monads::Just.new(3)
monad.or(-> { exclude }).should eq(exclude)
end
end

describe "#bind" do
Expand Down
8 changes: 8 additions & 0 deletions src/monads/maybe.cr
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ module Monads
Just.new(value!)
end

def or(lambda : -> _) : Maybe(T)
Just.new(value!)
end

def map_or(default : U, lambda : T -> U) forall U
lambda.call(value!)
end
Expand Down Expand Up @@ -99,6 +103,10 @@ module Monads
default
end

def or(lambda : -> _)
lambda.call
end

def map_or(default : U, lambda : _ -> _) forall U
default
end
Expand Down

0 comments on commit 0dec86e

Please sign in to comment.