Sha256: 5a5b4a342bbc7491b7d5315e7d96fc8e333a15b4d101007d250986af825d7bdd

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module PropCheck
  ##
  # Helper functions that have no other place to live
  module Helper
    extend self
    ##
    # Creates a (potentially lazy) Enumerator
    # starting with `elem`
    # with each consecutive element obtained
    # by calling `operation` on the previous element.
    #
    #   >> Helper.scanl(0, &:next).take(10).force
    #   => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    #   >> Helper.scanl([0, 1]) { |curr, next_elem| [next_elem, curr + next_elem] }.map(&:first).take(10).force
    #   => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
    def scanl(elem, &operation)
      Enumerator.new do |yielder|
        acc = elem
        loop do
          # p acc
          yielder << acc
          acc = operation.call(acc)
        end
      end.lazy
    end

    ##
    # allow lazy appending of two (potentially lazy) enumerators:
    #   >> PropCheck::Helper::LazyAppend.lazy_append([1,2,3],[4,5.6]).to_a
    #   => [1,2,3,4,5,6]
    def lazy_append(this_enumerator, other_enumerator)
      [this_enumerator, other_enumerator].lazy.flat_map(&:lazy)
    end

    def call_splatted(val, &block)
      return block.call(**val) if val.is_a?(Hash) && val.keys.all? { |k| k.is_a?(Symbol) }

      block.call(val)
      # if kwval != {}
      #   block.call(**kwval)
      # else
      #   block.call(*val)
      # end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prop_check-0.15.0 lib/prop_check/helper.rb