class Fear::PartialFunction::Guard

Guard represents PartialFunction guardian

@api private

Attributes

condition[R]

Public Class Methods

and(conditions) click to toggle source

@param conditions [<#===>] @return [Fear::PartialFunction::Guard]

# File lib/fear/partial_function/guard.rb, line 32
def and(conditions)
  case conditions.size
  when 1 then and1(*conditions)
  when 2 then and2(*conditions)
  when 3 then and3(*conditions)
  when 0 then Any
  else
    head, *tail = conditions
    tail.reduce(new(head)) { |acc, condition| acc.and(new(condition)) }
  end
end
and1(c) click to toggle source
# File lib/fear/partial_function/guard.rb, line 26
def and1(c)
  c
end
and2(c1, c2) click to toggle source

Optimized version for combination of two guardians Two guarding is a very common situation. For example checking for Some, and checking a value withing container.

# File lib/fear/partial_function/guard.rb, line 18
def and2(c1, c2)
  Guard::And.new(c1, c2)
end
and3(c1, c2, c3) click to toggle source
# File lib/fear/partial_function/guard.rb, line 22
def and3(c1, c2, c3)
  Guard::And3.new(c1, c2, c3)
end
new(condition) click to toggle source

@param condition [#===]

# File lib/fear/partial_function/guard.rb, line 55
def initialize(condition)
  @condition = condition
end
or(conditions) click to toggle source

@param conditions [<#===>] @return [Fear::PartialFunction::Guard]

# File lib/fear/partial_function/guard.rb, line 46
def or(conditions)
  return Any if conditions.empty?

  head, *tail = conditions
  tail.reduce(new(head)) { |acc, condition| acc.or(new(condition)) }
end

Public Instance Methods

===(arg) click to toggle source

@param arg [any] @return [Boolean]

# File lib/fear/partial_function/guard.rb, line 75
def ===(arg)
  condition === arg
end
and(other) click to toggle source

@param other [Fear::PartialFunction::Guard] @return [Fear::PartialFunction::Guard]

# File lib/fear/partial_function/guard.rb, line 63
def and(other)
  Guard::And.new(condition, other)
end
or(other) click to toggle source

@param other [Fear::PartialFunction::Guard] @return [Fear::PartialFunction::Guard]

# File lib/fear/partial_function/guard.rb, line 69
def or(other)
  Guard::Or.new(condition, other)
end