source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/unique.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 8 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

File size: 1.0 KB
Line 
1#
2# unique.rb
3#
4
5module Puppet::Parser::Functions
6 newfunction(:unique, :type => :rvalue, :doc => <<-EOS
7This function will remove duplicates from strings and arrays.
8
9*Examples:*
10
11 unique("aabbcc")
12
13Will return:
14
15 abc
16
17You can also use this with arrays:
18
19 unique(["a","a","b","b","c","c"])
20
21This returns:
22
23 ["a","b","c"]
24 EOS
25 ) do |arguments|
26
27 raise(Puppet::ParseError, "unique(): Wrong number of arguments " +
28 "given (#{arguments.size} for 1)") if arguments.size < 1
29
30 value = arguments[0]
31
32 unless value.is_a?(Array) || value.is_a?(String)
33 raise(Puppet::ParseError, 'unique(): Requires either ' +
34 'array or string to work with')
35 end
36
37 result = value.clone
38
39 string = value.is_a?(String) ? true : false
40
41 # We turn any string value into an array to be able to shuffle ...
42 result = string ? result.split('') : result
43 result = result.uniq # Remove duplicates ...
44 result = string ? result.join : result
45
46 return result
47 end
48end
49
50# vim: set ts=2 sw=2 et :
Note: See TracBrowser for help on using the repository browser.