Module: SumsUp
- Extended by:
- Forwardable
- Defined in:
- lib/sums_up.rb,
lib/sums_up/core.rb,
lib/sums_up/maybe.rb,
lib/sums_up/result.rb,
lib/sums_up/version.rb,
lib/sums_up/core/parser.rb,
lib/sums_up/core/matcher.rb,
lib/sums_up/core/strings.rb,
lib/sums_up/core/variant.rb,
lib/sums_up/core/sum_type.rb,
lib/sums_up/core/functions.rb
Overview
UI-level functions for the gem.
Defined Under Namespace
Modules: Core
Constant Summary collapse
- Error =
Class.new(StandardError)
- MatchError =
Class.new(Error)
- UnmatchedVariantError =
Class.new(MatchError)
- MatchAfterWildcardError =
Class.new(MatchError)
- DuplicateMatchError =
Class.new(MatchError)
- UnknownVariantError =
Class.new(MatchError)
- ParserError =
Class.new(Error)
- VariantNameError =
Class.new(ParserError)
- VariantArgsError =
Class.new(ParserError)
- DuplicateNameError =
Class.new(ParserError)
- Maybe =
To paraphrase Haskell’s Data.Maybe docs:
Maybe represents an optional value. A Maybe value contains either a value (Maybe.just(1)), or is empty (Maybe.nothing).
SumsUp.define(:nothing, just: :value) do # Build a new Maybe from a value which may or may not be nil. def self.of(value) if value.nil? nothing else just(value) end end def chain match do |m| m.just { |value| yield(value) } m.nothing self end end alias_method(:flat_map, :chain) # Map a function across the Maybe. If present, the value is yielded and that # result is wrapped in a new Maybe.just. Returns Maybe.nothing otherwise. def map match do |m| m.just { |value| Maybe.just(yield(value)) } m.nothing Maybe.nothing end end # On nothing, return the provided default value (or yield). On just, return # the value. def or_else(default = nil) match do |m| m.just { |value| value } m.nothing do block_given? ? yield : default end end end end
- Result =
To parahprase Rust’s std::result docs:
Result is a type used for returning and propagating errors. Result.success(value) represents a success and contains a value; Result.failure(error) represents an error with a propagated error.
SumsUp.define(failure: :error, success: :value) do # Yield, wrapping the result in Result.success, or wrap the raised error # in Result.failure. def self.from_block(error_class = StandardError) success(yield) rescue error_class => e failure(e) end def chain match do |m| m.success { |value| yield(value) } m.failure self end end alias_method(:flat_map, :chain) # Map a function across the successful value (if present). def map match do |m| m.success { |value| Result.success(yield(value)) } m.failure self end end # Map a function across the error (if present). def map_failure match do |m| m.success self m.failure { |error| Result.failure(yield(error)) } end end end
- VERSION =
"1.2.2"