Skip to content

Commit

Permalink
Merge pull request #153 from dry-rb/alias-right-biased-or
Browse files Browse the repository at this point in the history
Add RightBiased#|
  • Loading branch information
flash-gordon authored Jul 19, 2021
2 parents db31ca5 + 22fe1e7 commit b452332
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
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

0 comments on commit b452332

Please sign in to comment.