Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some methods and remove some methods #3

Merged
merged 4 commits into from
Apr 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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