Skip to content

Commit 1185cd5

Browse files
committed
(PUP-2794) Change function reduce to use dispatchers for block arity
This changes the reduce function to use the dispatchers to check the block arity.
1 parent 53b94f2 commit 1185cd5

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

lib/puppet/functions/reduce.rb

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,38 +65,30 @@
6565

6666
dispatch :reduce_without_memo do
6767
param 'Any', :enumerable
68-
required_block_param
68+
required_block_param 'Callable[2,2]', :block
6969
end
7070

7171
dispatch :reduce_with_memo do
7272
param 'Any', :enumerable
7373
param 'Any', :memo
74-
required_block_param
74+
required_block_param 'Callable[2,2]', :block
7575
end
7676

77-
require 'puppet/util/functions/iterative_support'
78-
include Puppet::Util::Functions::IterativeSupport
79-
8077
def reduce_without_memo(enumerable, pblock)
81-
assert_serving_size(pblock)
8278
enum = asserted_enumerable(enumerable)
8379
enum.reduce {|memo, x| pblock.call(nil, memo, x) }
8480
end
8581

8682
def reduce_with_memo(enumerable, given_memo, pblock)
87-
assert_serving_size(pblock)
8883
enum = asserted_enumerable(enumerable)
8984
enum.reduce(given_memo) {|memo, x| pblock.call(nil, memo, x) }
9085
end
9186

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.")
10090
end
91+
enum
10192
end
93+
10294
end

spec/unit/functions/reduce_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,5 @@
8989
expect(catalog).to have_resource("File[/file_sum_10]").with_parameter(:ensure, 'present')
9090
end
9191
end
92+
9293
end

0 commit comments

Comments
 (0)