File: flatten.rb

package info (click to toggle)
puppet-agent 8.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,404 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (66 lines) | stat: -rw-r--r-- 1,581 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

# 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