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

Add the tree iterative function tree_each

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: PUP 5.1.0
    • Component/s: Language
    • Labels:
      None
    • Template:
    • Epic Link:
    • Sub-team:
    • Team:
      Agent
    • Story Points:
      3
    • Sprint:
      Agent 2017-06-28
    • Release Notes:
      New Feature
    • Release Notes Summary:
      Hide
      The function {{tree_each}} has been added to allow convenient iteration, filtering, mapping, and reduction over complex structures. The function iterates recursively in a way that flattens a structure into a sequence, but that retains information about the structure thus making it possible to put it back together with possibly altered values. This was a difficult operation to do in Puppet Language earlier and required a user to write a custom recursive function to achieve the same result.
      Show
      The function {{tree_each}} has been added to allow convenient iteration, filtering, mapping, and reduction over complex structures. The function iterates recursively in a way that flattens a structure into a sequence, but that retains information about the structure thus making it possible to put it back together with possibly altered values. This was a difficult operation to do in Puppet Language earlier and required a user to write a custom recursive function to achieve the same result.
    • QA Risk Assessment:
      No Action
    • QA Risk Assessment Reason:
      Covered by spec tests

      Description

      UPDATE


      A simpler solution was implemented. The function tree_each which is configurable to iterate over Array, Hash and Object data types (the type of container can be selected), yielding elements that represents a "flattened tree". This enables other existing iterable functions to be used to map, filter, reduce (etc.) the result.
      At the end, a tree can again be constructed by creating a new Hash using Hash(<flattended_tree>, 'tree'). For details see the documentation in the new function, and in the documentation for tree_each.

      The Original ticket text is retained below (mostly for its motivation why the function is wanted).

      ORIGINAL


      Background


      At the moment it is difficult to flatten out a complex structure. There is a flatten in standard lib that creates a flat copy of a tree of Array objects, and it errors if given a Hash. If the restriction was lifted to also support Hash, Ruby flatten method flattens a hash by making an array of keys and values. That makes the result hard to use if it is a mix of arrays and hashes.

      It is very useful to be able to iterate over such a tree instead of creating a copy, and it is of value to iterate over it in different ways. This is what the flat_each does.

      To describe what is wanted; imagine the two functions each_element, and each_container (these are not the functions we want at the end; they are too special, but they serve as illustration).

      each_element(Iterable $x)
      This function visits each container and leaf in the tree $x and calls a given block with 1.3 parameters. The block gets container, index, and value if defining three parameters, if using 2 arguments the container and index is delivered as a tuple, and if using 1 arg the three args are delivered as a Triple. The first yielded value is the given argument value. Each element in the tree is yielded with parent before children, depth first.

      When not given a block, each_element produces an Iterator that yields a triplet.

      each_container(Iterable $x)
      This function is he same as to each_element but skips children that are not containers. This enables doing inner loops conditionally.

      What we should do instead is to first implement next() since that makes it possible to do general skipping and pruning; next(true) means skip this element and its children and next(), or next(false) to mean "do not prune", take next element I am done with the one you just gave me.

        Attachments

          Activity

            jsd-sla-details-panel

              People

              • Assignee:
                Unassigned
                Reporter:
                henrik.lindberg Henrik Lindberg
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: