Details
-
Improvement
-
Status: Closed
-
Normal
-
Resolution: Duplicate
-
None
-
None
-
None
-
Froyo
-
New Feature
-
The merge() function in stdlib has moved to puppet and can be used to dynamically build a hash from an array or other hash. This enables building hashes dynamically without the performance issues inherent when using the {{reduce}} function to do the same.
-
Needs Assessment
Description
I think we should migrate the merge() function from stdlib to puppet and teach it new tricks.
Currently it only merges two hashes - it should instead be allowed to iterate over a hash or array and yield to a lambda that computes entries to merge - either by returning a hash, or an array that can be converted to a Hash. Returning undef adds nothing.
This is of value since the only option in puppet to iteratively building a hash is via the reduce function (or by first creating an array - with similar issues) and it does not perform well if used to build a large hash (it will create a new copy of the hash it is building on each iteration).
The lambda should be called with the hash built to this point (to allow reading it).
$input = [a, b, c, d, c, b, b, a] |
$input.merge |$hsh, $val| { { $val => $hsh[$val].lest || { 0 } + 1} } |
Would build a hash with a count per occurence of string in an array. The example would produce:
{ a => 2, b => 3, c=> 2, d => 1 }
|
Attachments
Issue Links
- duplicates
-
MODULES-8760 The merge() function should be able to merge iteratively
-
- Closed
-
- relates to
-
PUP-9561 puppet language functions are 11,350x slower than ruby functions
-
- Resolved
-