Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / intersection.rb
1 #
2 # intersection.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:intersection, :type => :rvalue, :doc => <<-EOS
7 This function returns an array of the intersection of two.
8
9 *Examples:*
10
11     intersection(["a","b","c"],["b","c","d"])  # returns ["b","c"]
12     intersection(["a","b","c"],[1,2,3,4])      # returns [] (true, when evaluated as a Boolean)
13
14     EOS
15   ) do |arguments|
16
17     # Two arguments are required
18     raise(Puppet::ParseError, "intersection(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size != 2
19
20     first = arguments[0]
21     second = arguments[1]
22
23     unless first.is_a?(Array) && second.is_a?(Array)
24       raise(Puppet::ParseError, 'intersection(): Requires 2 arrays')
25     end
26
27     result = first & second
28
29     return result
30   end
31 end
32
33 # vim: set ts=2 sw=2 et :