Sha256: 3908f97ae01a87314d97abd518e7e8ed64f9032428f73b8ace3a280d116cdda8

Contents?: true

Size: 730 Bytes

Versions: 9

Compression:

Stored size: 730 Bytes

Contents

module Predicates
  private
  def predicate(fn)
    def fn.and(other)
      -> (value) { self.(value) && other.(value) }
    end
    def fn.or(other)
      -> (value) { self.(value) || other.(value) }
    end
    fn
  end

  def is_not(pred)
    predicate(-> (bool) { !pred.(bool) })
  end

  def matches(regex)
    predicate(->(value) { !regex.match(value).nil? })
  end

  def equal_to?(that)
    predicate(->(this) { this == that })
  end
  alias is equal_to?

  def where(fn, predicate)
    predicate(->(value) { predicate.(fn.(value)) })
  end
end

class PredicateFunction < Proc
  def and(other)
    ->(value) { self.(value) && other.(value) }
  end

  def or(other)
    ->(value) { self.(value) || other.(value) }
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
totally_lazy-0.1.62 lib/totally_lazy/predicates.rb
totally_lazy-0.1.61 lib/totally_lazy/predicates.rb
totally_lazy-0.1.60 lib/totally_lazy/predicates.rb
totally_lazy-0.1.59 lib/totally_lazy/predicates.rb
totally_lazy-0.1.58 lib/totally_lazy/predicates.rb
totally_lazy-0.1.57 lib/totally_lazy/predicates.rb
totally_lazy-0.1.56 lib/totally_lazy/predicates.rb
totally_lazy-0.1.55 lib/totally_lazy/predicates.rb
totally_lazy-0.1.54 lib/totally_lazy/predicates.rb