Skip to content

Commit

Permalink
Move pattern match files out of the main namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
bolshakov committed Apr 2, 2024
1 parent 30b3305 commit d81582d
Show file tree
Hide file tree
Showing 33 changed files with 259 additions and 233 deletions.
8 changes: 4 additions & 4 deletions lib/fear/either.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ module Fear
#
# @!method match(&matcher)
# Pattern match against this +Either+
# @yield matcher [Fear::EitherPatternMatch]
# @yield matcher [Fear::Either::PatternMatch]
# @example
# either.match do |m|
# m.right(Integer) do |x|
Expand Down Expand Up @@ -309,10 +309,10 @@ class << self
# end
# matcher.call(Fear.right(42))
#
# @yieldparam [Fear::EitherPatternMatch]
# @yieldparam [Fear::Either::PatternMatch]
# @return [Fear::PartialFunction]
def matcher(&matcher)
EitherPatternMatch.new(&matcher)
Either::PatternMatch.new(&matcher)
end
end

Expand Down Expand Up @@ -345,7 +345,7 @@ def Right(value)
end
end

require "fear/either_pattern_match"
require "fear/either/pattern_match"
require "fear/left"
require "fear/right"
require "fear/either/left_projection"
51 changes: 51 additions & 0 deletions lib/fear/either/pattern_match.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require "fear/pattern_match"

module Fear
module Either
# Either pattern matcher
#
# @example
# pattern_match =
# EitherPatternMatch.new
# .right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
# .right(String) { |x| x.to_i * 2 }
# .left(String) { :err }
# .else { 'error '}
#
# pattern_match.call(42) => 'NaN'
#
# @example the same matcher may be defined using block syntax
# EitherPatternMatch.new do |m|
# m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
# m.right(String) { |x| x.to_i * 2 }
# m.left(String) { :err }
# m.else { 'error '}
# end
#
# @note it has two optimized subclasses +Fear::Left::PatternMatch+ and +Fear::Right::PatternMatch+
# @api private
class PatternMatch < Fear::PatternMatch
# Match against +Fear::Right+
#
# @param conditions [<#==>]
# @return [Fear::Either::PatternMatch]
def right(*conditions, &effect)
branch = Fear.case(Fear::Right, &:right_value).and_then(Fear.case(*conditions, &effect))
or_else(branch)
end
alias success right

# Match against +Fear::Left+
#
# @param conditions [<#==>]
# @return [Fear::Either::PatternMatch]
def left(*conditions, &effect)
branch = Fear.case(Fear::Left, &:left_value).and_then(Fear.case(*conditions, &effect))
or_else(branch)
end
alias failure left
end
end
end
52 changes: 0 additions & 52 deletions lib/fear/either_pattern_match.rb

This file was deleted.

4 changes: 3 additions & 1 deletion lib/fear/failure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Fear
class Failure
include Try
include RightBiased::Left
include FailurePatternMatch.mixin
include Failure::PatternMatch.mixin

# @param [StandardError]
def initialize(exception)
Expand Down Expand Up @@ -103,3 +103,5 @@ def deconstruct
end
end
end

require "fear/failure/pattern_match"
14 changes: 14 additions & 0 deletions lib/fear/failure/pattern_match.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Fear
class Failure
# @api private
class PatternMatch < Try::PatternMatch
def success(*_conditions)
self
end
end

private_constant :PatternMatch
end
end
12 changes: 0 additions & 12 deletions lib/fear/failure_pattern_match.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/fear/future.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def on_complete
#
# If the future has already been completed,
# this will either be applied immediately or be scheduled asynchronously.
# @yieldparam [Fear::TryPatternMatch]
# @yieldparam [Fear::Try::PatternMatch]
# @return [self]
#
# @example
Expand Down
4 changes: 3 additions & 1 deletion lib/fear/left.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Fear
class Left
include Either
include RightBiased::Left
include LeftPatternMatch.mixin
include Left::PatternMatch.mixin

# @api private
def left_value
Expand Down Expand Up @@ -74,3 +74,5 @@ def ===(other)
end
end
end

require "fear/left/pattern_match"
15 changes: 15 additions & 0 deletions lib/fear/left/pattern_match.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Fear
class Left
# @api private
class PatternMatch < Fear::Either::PatternMatch
def right(*)
self
end
alias success right
end

private_constant :PatternMatch
end
end
11 changes: 0 additions & 11 deletions lib/fear/left_pattern_match.rb

This file was deleted.

4 changes: 3 additions & 1 deletion lib/fear/none.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Fear
class NoneClass
include Option
include RightBiased::Left
include NonePatternMatch.mixin
include NoneClass::PatternMatch.mixin

# @raise [NoSuchElementError]
def get
Expand Down Expand Up @@ -92,3 +92,5 @@ def inherited(*)
end
end
end

require "fear/none_class/pattern_match"
16 changes: 16 additions & 0 deletions lib/fear/none_class/pattern_match.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Fear
class NoneClass
# @api private
class PatternMatch < Option::PatternMatch
# @param conditions [<#==>]
# @return [Fear::OptionPatternMatch]
def some(*conditions)
self
end
end

private_constant :PatternMatch
end
end
14 changes: 0 additions & 14 deletions lib/fear/none_pattern_match.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/fear/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class << self
# @yieldparam [OptionPatternMatch]
# @return [Fear::PartialFunction]
def matcher(&matcher)
OptionPatternMatch.new(&matcher)
Option::PatternMatch.new(&matcher)
end

def match(value, &block)
Expand Down Expand Up @@ -259,6 +259,6 @@ def Some(value)
end
end

require "fear/option_pattern_match"
require "fear/option/pattern_match"
require "fear/some"
require "fear/none"
49 changes: 49 additions & 0 deletions lib/fear/option/pattern_match.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require "fear/pattern_match"

module Fear
module Option
# Option pattern matcher
#
# @example
# pattern_match =
# Option::PatternMatch.new
# .some(Integer) { |x| x * 2 }
# .some(String) { |x| x.to_i * 2 }
# .none { 'NaN' }
# .else { 'error '}
#
# pattern_match.call(42) => 'NaN'
#
# @example the same matcher may be defined using block syntax
# Option::PatternMatch.new do |m|
# m.some(Integer) { |x| x * 2 }
# m.some(String) { |x| x.to_i * 2 }
# m.none { 'NaN' }
# m.else { 'error '}
# end
#
# @note it has two optimized subclasses +Fear::SomePatternMatch+ and +Fear::NonePatternMatch+
# @api private
class PatternMatch < Fear::PatternMatch
# Match against Some
#
# @param conditions [<#==>]
# @return [Fear::Option::PatternMatch]
def some(*conditions, &effect)
branch = Fear.case(Fear::Some, &:get).and_then(Fear.case(*conditions, &effect))
or_else(branch)
end

# Match against None
#
# @param effect [Proc]
# @return [Fear::Option::PatternMatch]
def none(&effect)
branch = Fear.case(Fear::None, &effect)
or_else(branch)
end
end
end
end
50 changes: 0 additions & 50 deletions lib/fear/option_pattern_match.rb

This file was deleted.

Loading

0 comments on commit d81582d

Please sign in to comment.