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 RightBiased#| #153

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
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
21 changes: 20 additions & 1 deletion lib/dry/monads/right_biased.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module RightBiased
# Right part
#
# @api public
module Right
module Right # rubocop:disable Metrics/ModuleLength
# @private
def self.included(m)
super
Expand Down Expand Up @@ -90,6 +90,16 @@ def or(*)
self
end

# Ignores arguments and returns self. It exists to keep the interface
# identical to that of {RightBiased::Left}.
#
# @param _alt [RightBiased::Right, RightBiased::Left]
#
# @return [RightBiased::Right]
def |(_alt)
self
end

# A lifted version of `#or`. For {RightBiased::Right} acts in the same way as `#or`,
# that is returns itselt.
#
Expand Down Expand Up @@ -299,6 +309,15 @@ def or(*)
raise NotImplementedError
end

# Returns the passed value. Works in pair with {RightBiased::Right#|}.
#
# @param alt [RightBiased::Right, RightBiased::Left]
#
# @return [RightBiased::Right, RightBiased::Left]
def |(alt)
self.or(alt)
end

# A lifted version of `#or`. This is basically `#or` + `#fmap`.
#
# @example
Expand Down
8 changes: 8 additions & 0 deletions spec/unit/maybe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,14 @@
expect(none.filter).to eql(none)
end
end

describe "#|" do
it "chooses the right value" do
expect(none | none | some[3]).to eql(some[3])
expect(none | some[3] | none | some[3]).to eql(some[3])
expect(none | none | none).to eql(none)
end
end
end

describe maybe::Mixin do
Expand Down
8 changes: 8 additions & 0 deletions spec/unit/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -599,5 +599,13 @@
)
end
end

describe "#|" do
it "chooses the right value" do
expect(failure.("foo") | failure.("bar") | success.("baz")).to eql(success.("baz"))
expect(failure.("foo") | failure.("bar") | failure.("baz")).to eql(failure.("baz"))
expect(success.("baz") | failure.("foo") | failure.("bar")).to eql(success.("baz"))
end
end
end
end