Module: PropCheck::Helper

Extended by:
Helper
Included in:
Helper
Defined in:
lib/prop_check/helper.rb

Overview

Helper functions that have no other place to live

Instance Method Summary collapse

Instance Method Details

#call_splatted(val, &block) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/prop_check/helper.rb', line 35

def call_splatted(val, &block)
  # Handle edge case where Ruby >= 3 behaves differently than Ruby <= 2
  # c.f. https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/#other-minor-changes-empty-hash
  return block.call({}) if val.is_a?(Hash) && val.empty?
  return block.call(**val) if val.is_a?(Hash) && val.keys.all? { |k| k.is_a?(Symbol) }

  block.call(val)
end

#lazy_append(this_enumerator, other_enumerator) ⇒ Object

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]


31
32
33
# File 'lib/prop_check/helper.rb', line 31

def lazy_append(this_enumerator, other_enumerator)
  [this_enumerator, other_enumerator].lazy.flat_map(&:lazy)
end

#scanl(elem, &operation) ⇒ Object

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]


16
17
18
19
20
21
22
23
24
25
# File 'lib/prop_check/helper.rb', line 16

def scanl(elem, &operation)
  Enumerator.new do |yielder|
    acc = elem
    loop do
      # p acc
      yielder << acc
      acc = operation.call(acc)
    end
  end.lazy
end