|
| 1 | +# Merges two or more hashes together or hashes resulting from iteration, and returns the resulting hash. |
| 2 | +# |
| 3 | +# @example Using merge() |
| 4 | +# |
| 5 | +# $hash1 = {'one' => 1, 'two', => 2} |
| 6 | +# $hash2 = {'two' => 'dos', 'three', => 'tres'} |
| 7 | +# $merged_hash = merge($hash1, $hash2) |
| 8 | +# # The resulting hash is equivalent to: |
| 9 | +# # $merged_hash = {'one' => 1, 'two' => 'dos', 'three' => 'tres'} |
| 10 | +# |
| 11 | +# When there is a duplicate key, the key in the rightmost hash will "win." |
| 12 | +# |
| 13 | +# Note that since Puppet 4.0.0 the same merge can be achieved with the + operator. |
| 14 | +# |
| 15 | +# $merged_hash = $hash1 + $hash2 |
| 16 | +# |
| 17 | +# If merge is given a single Iterable (Array, Hash, etc.) it will call a given block with |
| 18 | +# up to three parameters, and merge each resulting Hash into the accumulated result. All other types |
| 19 | +# of values returned from the block (typically undef) are skipped (not merged). |
| 20 | +# |
| 21 | +# The codeblock can take 2 or three parameters: |
| 22 | +# * with two, it gets the current hash (as built to this point), and each value (for hash the value is a [key, value] tuple) |
| 23 | +# * with three, it gets the current hash (as built to this point), the key/index of each value, and then the value |
| 24 | +# |
| 25 | +# If the iterable is empty, or no hash was returned from the given block, an empty hash is returned. In the given block, a call to `next()` |
| 26 | +# will skip that entry, and a call to `break()` will end the iteration. |
| 27 | +# |
| 28 | +# @example counting occurrences of strings in an array |
| 29 | +# ['a', 'b', 'c', 'c', 'd', 'b'].merge | $hsh, $v | { { $v => $hsh[$v].lest || { 0 } + 1 } } |
| 30 | +# # would result in { a => 1, b => 2, c => 2, d => 1 } |
| 31 | +# |
| 32 | +# @example skipping values for entries that are longer than 1 char |
| 33 | +# ['a', 'b', 'c', 'c', 'd', 'b', 'blah', 'blah'].merge | $hsh, $v | { if $v =~ String[1,1] { { $v => $hsh[$v].lest || { 0 } + 1 } } } |
| 34 | +# # would result in { a => 1, b => 2, c => 2, d => 1 } since 'blah' is longer than 2 chars |
| 35 | +# |
| 36 | +# The iterative `merge()` has an advantage over doing the same with a general `reduce()` in that the constructed hash |
| 37 | +# does not have to be copied in each iteration and thus will perform much better with large inputs. |
| 38 | +# |
| 39 | +Puppet::Functions.create_function(:merge) do |
| 40 | + dispatch :merge2hashes do |
| 41 | + repeated_param 'Variant[Hash, Undef, String[0,0]]', :args # this strange type is backwards compatible |
| 42 | + return_type 'Hash' |
| 43 | + end |
| 44 | + |
| 45 | + dispatch :merge_iterable3 do |
| 46 | + repeated_param 'Iterable', :args |
| 47 | + block_param 'Callable[3,3]', :block |
| 48 | + return_type 'Hash' |
| 49 | + end |
| 50 | + |
| 51 | + dispatch :merge_iterable2 do |
| 52 | + repeated_param 'Iterable', :args |
| 53 | + block_param 'Callable[2,2]', :block |
| 54 | + return_type 'Hash' |
| 55 | + end |
| 56 | + |
| 57 | + def merge2hashes(*hashes) |
| 58 | + accumulator = {} |
| 59 | + hashes.each { |h| accumulator.merge!(h) if h.is_a?(Hash) } |
| 60 | + accumulator |
| 61 | + end |
| 62 | + |
| 63 | + def merge_iterable2(iterable) |
| 64 | + accumulator = {} |
| 65 | + enum = Puppet::Pops::Types::Iterable.asserted_iterable(self, iterable) |
| 66 | + enum.each do |v| |
| 67 | + r = yield(accumulator, v) |
| 68 | + accumulator.merge!(r) if r.is_a?(Hash) |
| 69 | + end |
| 70 | + accumulator |
| 71 | + end |
| 72 | + |
| 73 | + def merge_iterable3(iterable) |
| 74 | + accumulator = {} |
| 75 | + enum = Puppet::Pops::Types::Iterable.asserted_iterable(self, iterable) |
| 76 | + if enum.hash_style? |
| 77 | + enum.each do |entry| |
| 78 | + r = yield(accumulator, *entry) |
| 79 | + accumulator.merge!(r) if r.is_a?(Hash) |
| 80 | + end |
| 81 | + else |
| 82 | + begin |
| 83 | + index = 0 |
| 84 | + loop do |
| 85 | + r = yield(accumulator, index, enum.next) |
| 86 | + accumulator.merge!(r) if r.is_a?(Hash) |
| 87 | + index += 1 |
| 88 | + end |
| 89 | + rescue StopIteration # rubocop:disable Lint/HandleExceptions |
| 90 | + end |
| 91 | + end |
| 92 | + accumulator |
| 93 | + end |
| 94 | +end |
0 commit comments