From 737aa31546e71e9febea2199582510ef88a2560c Mon Sep 17 00:00:00 2001 From: Alex Cline Date: Mon, 13 May 2013 12:09:33 -0400 Subject: (#20684) Add array comparison functions, difference, intersection and union. Included is code, tests and documentation for the difference, intersection and union functions for comparing arrays. --- lib/puppet/parser/functions/intersection.rb | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 lib/puppet/parser/functions/intersection.rb (limited to 'lib/puppet/parser/functions/intersection.rb') diff --git a/lib/puppet/parser/functions/intersection.rb b/lib/puppet/parser/functions/intersection.rb new file mode 100644 index 0000000..48f02e9 --- /dev/null +++ b/lib/puppet/parser/functions/intersection.rb @@ -0,0 +1,34 @@ +# +# intersection.rb +# + +module Puppet::Parser::Functions + newfunction(:intersection, :type => :rvalue, :doc => <<-EOS +This function returns an array an intersection of two. + +*Examples:* + + intersection(["a","b","c"],["b","c","d"]) + +Would return: ["b","c"] + EOS + ) do |arguments| + + # Two arguments are required + raise(Puppet::ParseError, "intersection(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size != 2 + + first = arguments[0] + second = arguments[1] + + unless first.is_a?(Array) && second.is_a?(Array) + raise(Puppet::ParseError, 'intersection(): Requires 2 arrays') + end + + result = first & second + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3