Sha256: 22d9ebde6afe0ffbbc22da1101e1f1d5eaa8033988adff5c002535d508598bbb

Contents?: true

Size: 1.51 KB

Versions: 447

Compression:

Stored size: 1.51 KB

Contents

# Returns a flat Array produced from its possibly deeply nested given arguments.
#
# One or more arguments of any data type can be given to this function.
# The result is always a flat array representation where any nested arrays are recursively flattened.
#
# @example Typical use of `flatten()`
#
# ```puppet
# flatten(['a', ['b', ['c']]])
# # Would return: ['a','b','c']
# ```
#
# To flatten other kinds of iterables (for example hashes, or intermediate results like from a `reverse_each`)
# first convert the result to an array using `Array($x)`, or `$x.convert_to(Array)`. See the `new` function
# for details and options when performing a conversion.
#
# @example Flattening a Hash
#
# ```puppet
# $hsh = { a => 1, b => 2}
#
# # -- without conversion
# $hsh.flatten()
# # Would return [{a => 1, b => 2}]
#
# # -- with conversion
# $hsh.convert_to(Array).flatten()
# # Would return [a,1,b,2]
#
# flatten(Array($hsh))
# # Would also return [a,1,b,2]
# ```
#
# @example Flattening and concatenating at the same time
#
# ```puppet
# $a1 = [1, [2, 3]]
# $a2 = [[4,[5,6]]
# $x = 7
# flatten($a1, $a2, $x)
# # would return [1,2,3,4,5,6,7]
# ```
#
# @example Transforming to Array if not already an Array
#
# ```puppet
# flatten(42)
# # Would return [42]
#
# flatten([42])
# # Would also return [42]
# ```
#
# @since 5.5.0 support for flattening and concatenating at the same time
#
Puppet::Functions.create_function(:flatten) do
  dispatch :flatten_args do
    repeated_param 'Any', :args
  end

  def flatten_args(*args)
    args.flatten()
  end
end

Version data entries

447 entries across 447 versions & 2 rubygems

Version Path
puppet-7.34.0 lib/puppet/functions/flatten.rb
puppet-7.34.0-x86-mingw32 lib/puppet/functions/flatten.rb
puppet-7.34.0-x64-mingw32 lib/puppet/functions/flatten.rb
puppet-7.34.0-universal-darwin lib/puppet/functions/flatten.rb
puppet-7.33.0 lib/puppet/functions/flatten.rb
puppet-7.33.0-x86-mingw32 lib/puppet/functions/flatten.rb
puppet-7.33.0-x64-mingw32 lib/puppet/functions/flatten.rb
puppet-7.33.0-universal-darwin lib/puppet/functions/flatten.rb
puppet-7.32.1 lib/puppet/functions/flatten.rb
puppet-7.32.1-x86-mingw32 lib/puppet/functions/flatten.rb
puppet-7.32.1-x64-mingw32 lib/puppet/functions/flatten.rb
puppet-7.32.1-universal-darwin lib/puppet/functions/flatten.rb
puppet-7.31.0 lib/puppet/functions/flatten.rb
puppet-7.31.0-x86-mingw32 lib/puppet/functions/flatten.rb
puppet-7.31.0-x64-mingw32 lib/puppet/functions/flatten.rb
puppet-7.31.0-universal-darwin lib/puppet/functions/flatten.rb
puppet-7.30.0 lib/puppet/functions/flatten.rb
puppet-7.30.0-x86-mingw32 lib/puppet/functions/flatten.rb
puppet-7.30.0-x64-mingw32 lib/puppet/functions/flatten.rb
puppet-7.30.0-universal-darwin lib/puppet/functions/flatten.rb