module Ytry::Try

Public Class Methods

ary_to_type(value) click to toggle source
# File lib/ytry.rb, line 14
def self.ary_to_type value
  raise Try.invalid_argument('Argument must be an array-like object', value) unless value.respond_to? :to_ary
  return value if value.is_a? Try
  value.to_ary.empty? ?
    Failure.new(RuntimeError.new("Element not found").tap{|ex| ex.set_backtrace(caller)}) :
    Success.new(value.to_ary.first)
end
zip(*try_array) click to toggle source
# File lib/ytry.rb, line 21
def self.zip *try_array
  first_failure = try_array.find(&:empty?)
  first_failure.nil? ? Success.new(try_array.map(&:get)) : first_failure
end

Private Class Methods

invalid_argument(type_str, arg) click to toggle source
# File lib/ytry.rb, line 95
def self.invalid_argument type_str, arg
  TypeError.new "#{type_str}. Found #{arg.class}"
end

Public Instance Methods

collect(&block)
Alias for: map
collect_concat(&block)
Alias for: flat_map
each() { |get| ... } click to toggle source
# File lib/ytry.rb, line 25
def each
  return enum_for(__method__) unless block_given?
  Try { yield self.get unless empty? }
  return self
end
Also aliased as: on_success
flat_map(&block) click to toggle source
# File lib/ytry.rb, line 52
def flat_map &block
  block or return enum_for(method)
  return self if self.empty?
  wrapped_result = Try{block.call(self.get)}
  return wrapped_result if (!wrapped_result.empty? && !wrapped_result.get.respond_to?(:to_ary))
  Try.ary_to_type(wrapped_result.flatten)
end
Also aliased as: collect_concat
flatten(level = 1) click to toggle source
# File lib/ytry.rb, line 67
def flatten level = 1
  level.times.reduce(self) { |current, _|
    break(current) if current.empty?
    Try.ary_to_type current.get
  }
end
get_or_else() { || ... } click to toggle source
# File lib/ytry.rb, line 88
def get_or_else
  raise ArgumentError, 'missing block' unless block_given?
  return self.get unless empty?
  yield
end
grep(pattern) { |v| ... } click to toggle source
# File lib/ytry.rb, line 60
def grep pattern
  return self if self.empty?
  match = Try {
    (pattern === self.get) ? self.get : (raise RuntimeError.new("Element not found"))
  }
  block_given? ? match.map{|v| yield v} : match
end
inspect() click to toggle source
# File lib/ytry.rb, line 93
def inspect() to_s end
map(&block) click to toggle source
# File lib/ytry.rb, line 35
def map &block
  block or return enum_for(__method__)
  self.empty? ? self : Try{block.call(self.get)}
end
Also aliased as: collect
on_failure() click to toggle source
# File lib/ytry.rb, line 31
def on_failure
  return enum_for(__method__) unless block_given?
  return self
end
on_success()
Alias for: each
or_else() { || ... } click to toggle source
# File lib/ytry.rb, line 79
def or_else
  return self unless empty?
  candidate = Try{ yield }
  if (!candidate.empty? && !candidate.get.is_a?(Try))
    raise(Try.invalid_argument('Block should evaluate to an instance of Try', candidate.get))
  else
    candidate.flatten
  end
end
reject(&block) click to toggle source
# File lib/ytry.rb, line 47
def reject &block
  if block_given? then select {|v| ! block.call(v)}
  else enum_for(__method__)
  end
end
select(&block) click to toggle source
# File lib/ytry.rb, line 40
def select &block
  block or return enum_for(__method__)
  return self if empty?
  predicate = Try{ block.call(self.get) }
  return predicate if predicate.empty?
  predicate.get ? self : Try.ary_to_type([])
end
zip(*others) click to toggle source
# File lib/ytry.rb, line 73
def zip *others
  Try.zip(self, *others)
end
|(lambda) click to toggle source
# File lib/ytry.rb, line 76
def | lambda
  self.flat_map &lambda # slow but easy to read + supports symbols out of the box
end