-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move pattern match files out of the main namespace
- Loading branch information
Showing
33 changed files
with
259 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.