Skip to content

Commit

Permalink
Merge pull request #3 from alex-lairan/add/methods
Browse files Browse the repository at this point in the history
Add some methods and remove some methods
moba1 authored Apr 20, 2019
2 parents 54f9b97 + b56517d commit fa8e8b7
Showing 3 changed files with 51 additions and 45 deletions.
31 changes: 13 additions & 18 deletions spec/monads/just_spec.cr
Original file line number Diff line number Diff line change
@@ -13,29 +13,17 @@ describe Monads::Just do
end
end

describe "#equal?" do
it "equal for same values" do
boolean = Monads::Just.new(1).equal?(Monads::Just.new(1))
boolean.should be_truthy
end

it "not equal for differents values" do
boolean = Monads::Just.new(1).equal?(Monads::Just.new(2))
boolean.should be_falsey
end
end

describe "#success?" do
it "is success" do
describe "#just?" do
it "verify that type is Just" do
boolean = Monads::Just.new(1)
boolean.success?.should be_truthy
boolean.just?.should be_truthy
end
end

describe "#failure?" do
it "is not failure" do
describe "#nothing?" do
it "verify that type is not Just" do
boolean = Monads::Just.new(1)
boolean.failure?.should be_falsey
boolean.nothing?.should be_falsey
end
end

@@ -97,4 +85,11 @@ describe Monads::Just do
expectation.should eq(1)
end
end

describe "#to_s" do
it "validate string" do
monad = Monads::Just.new(nil)
monad.to_s.should eq("#{typeof(monad)}{#{monad.value!.inspect}}")
end
end
end
31 changes: 13 additions & 18 deletions spec/monads/nothing_spec.cr
Original file line number Diff line number Diff line change
@@ -18,29 +18,17 @@ describe Monads::Nothing do
end
end

describe "#equal?" do
it "equal for same values" do
boolean = Monads::Nothing(String).new.equal?(Monads::Nothing(String).new)
boolean.should be_truthy
end

it "not equal for differents values" do
boolean = Monads::Nothing(Int32).new.equal?(Monads::Just.new(2))
boolean.should be_falsey
end
end

describe "#success?" do
it "is success" do
describe "#just?" do
it "verify that type is not Just" do
boolean = Monads::Nothing(Nil).new
boolean.success?.should be_falsey
boolean.just?.should be_falsey
end
end

describe "#failure?" do
it "is not failure" do
describe "#nothing?" do
it "verify that type is Just" do
boolean = Monads::Nothing(Nil).new
boolean.failure?.should be_truthy
boolean.nothing?.should be_truthy
end
end

@@ -103,4 +91,11 @@ describe Monads::Nothing do
# expectation.should eq(0)
end
end

describe "#to_s" do
it "validate string" do
monad = Monads::Nothing(Nil).new
monad.to_s.should eq("#{typeof(monad)}")
end
end
end
34 changes: 25 additions & 9 deletions src/monads/maybe.cr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Monads
abstract class Maybe(T)
def ==(rhs : RightBiased | LeftBiased) : Bool
equal?(rhs)
def just?
typeof(self) == Just(T)
end

abstract def success?
abstract def failure?
def nothing?
!just?
end

abstract def equal?(rhs : RightBiased | LeftBiased)
abstract def value!
abstract def value_or(element : U) forall U
abstract def value_or(&block : -> U) forall U
@@ -29,12 +29,16 @@ module Monads
Just(U).new(block.call(@data))
end

def equal?(rhs : Just(U)) : Bool forall U
@data == rhs.value!
def to_s
"#{typeof(self)}{#{value!.inspect}}"
end

def equal?(rhs : Nothing(U)) : Bool forall U
false
def inspect(io)
io << to_s
end

def ==(other : self) : Bool
other.value! == value!
end
end

@@ -52,5 +56,17 @@ module Monads
def equal?(rhs : Nothing(U)) : Bool forall U
typeof(self) == typeof(rhs)
end

def to_s
"#{typeof(self)}"
end

def inspect(io)
io << to_s
end

def ==(other : self) : Bool
typeof(self) == typeof(other)
end
end
end

0 comments on commit fa8e8b7

Please sign in to comment.