|
65 | 65 |
|
66 | 66 | dispatch :reduce_without_memo do |
67 | 67 | param 'Any', :enumerable |
68 | | - required_block_param |
| 68 | + required_block_param 'Callable[2,2]', :block |
69 | 69 | end |
70 | 70 |
|
71 | 71 | dispatch :reduce_with_memo do |
72 | 72 | param 'Any', :enumerable |
73 | 73 | param 'Any', :memo |
74 | | - required_block_param |
| 74 | + required_block_param 'Callable[2,2]', :block |
75 | 75 | end |
76 | 76 |
|
77 | | - require 'puppet/util/functions/iterative_support' |
78 | | - include Puppet::Util::Functions::IterativeSupport |
79 | | - |
80 | 77 | def reduce_without_memo(enumerable, pblock) |
81 | | - assert_serving_size(pblock) |
82 | 78 | enum = asserted_enumerable(enumerable) |
83 | 79 | enum.reduce {|memo, x| pblock.call(nil, memo, x) } |
84 | 80 | end |
85 | 81 |
|
86 | 82 | def reduce_with_memo(enumerable, given_memo, pblock) |
87 | | - assert_serving_size(pblock) |
88 | 83 | enum = asserted_enumerable(enumerable) |
89 | 84 | enum.reduce(given_memo) {|memo, x| pblock.call(nil, memo, x) } |
90 | 85 | end |
91 | 86 |
|
92 | | - # Asserts number of lambda parameters with more specific error message than the generic |
93 | | - # mis-matched arguments message that is produced by the dispatcher's type checking. |
94 | | - # |
95 | | - def assert_serving_size(pblock) |
96 | | - serving_size = pblock.parameter_count |
97 | | - unless serving_size == 2 || pblock.last_captures_rest? && serving_size <= 2 |
98 | | - raise ArgumentError, "reduce(): block must define 2 parameters; memo, value. Block has #{serving_size}; "+ |
99 | | - pblock.parameter_names.join(', ') |
| 87 | + def asserted_enumerable(obj) |
| 88 | + unless enum = Puppet::Pops::Types::Enumeration.enumerator(obj) |
| 89 | + raise ArgumentError, ("#{self.class.name}(): wrong argument type (#{obj.class}; must be something enumerable.") |
100 | 90 | end |
| 91 | + enum |
101 | 92 | end |
| 93 | + |
102 | 94 | end |
0 commit comments