Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / zip.rb
1 #
2 # zip.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:zip, :type => :rvalue, :doc => <<-EOS
7 Takes one element from first array and merges corresponding elements from second array. This generates a sequence of n-element arrays, where n is one more than the count of arguments.
8
9 *Example:*
10
11     zip(['1','2','3'],['4','5','6'])
12
13 Would result in:
14
15     ["1", "4"], ["2", "5"], ["3", "6"]
16     EOS
17   ) do |arguments|
18
19     # Technically we support three arguments but only first is mandatory ...
20     raise(Puppet::ParseError, "zip(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size < 2
21
22     a = arguments[0]
23     b = arguments[1]
24
25     unless a.is_a?(Array) and b.is_a?(Array)
26       raise(Puppet::ParseError, 'zip(): Requires array to work with')
27     end
28
29     flatten = function_str2bool([arguments[2]]) if arguments[2]
30
31     result = a.zip(b)
32     result = flatten ? result.flatten : result
33
34     return result
35   end
36 end
37
38 # vim: set ts=2 sw=2 et :