Uploaded image for project: 'Puppet'
  1. Puppet
  2. PUP-9568

Migrate merge() from stdlib and make it support iterative creation of hash

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed
    • Normal
    • Resolution: Duplicate
    • None
    • None
    • None
    • 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

          Activity

            People

              Unassigned Unassigned
              henrik.lindberg Henrik Lindberg
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Zendesk Support