~ubuntu-branches/ubuntu/wily/puppet/wily-proposed

« back to all changes in this revision

Viewing changes to lib/puppet/functions/map.rb

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2014-10-24 13:47:15 UTC
  • mfrom: (3.1.64 sid)
  • Revision ID: package-import@ubuntu.com-20141024134715-6ig54u0c4gar36ss
Tags: 3.7.2-1
* Imported upstream release 3.7.2
* Declare compliance with Debian Policy 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Applies a parameterized block to each element in a sequence of entries from the first
 
2
# argument and returns an array with the result of each invocation of the parameterized block.
 
3
#
 
4
# This function takes two mandatory arguments: the first should be an Array, Hash, or of Enumerable type
 
5
# (integer, Integer range, or String), and the second a parameterized block as produced by the puppet syntax:
 
6
#
 
7
#       $a.map |$x| { ... }
 
8
#       map($a) |$x| { ... }
 
9
#
 
10
# When the first argument `$a` is an Array or of enumerable type, the block is called with each entry in turn.
 
11
# When the first argument is a hash the entry is an array with `[key, value]`.
 
12
#
 
13
# @example Using map with two arguments
 
14
#
 
15
#      # Turns hash into array of values
 
16
#      $a.map |$x|{ $x[1] }
 
17
#
 
18
#      # Turns hash into array of keys
 
19
#      $a.map |$x| { $x[0] }
 
20
#
 
21
# When using a block with 2 parameters, the element's index (starting from 0) for an array, and the key for a hash
 
22
# is given to the block's first parameter, and the value is given to the block's second parameter.args.
 
23
#
 
24
# @example Using map with two arguments
 
25
#
 
26
#      # Turns hash into array of values
 
27
#      $a.map |$key,$val|{ $val }
 
28
#
 
29
#      # Turns hash into array of keys
 
30
#      $a.map |$key,$val|{ $key }
 
31
#
 
32
# @since 3.4 for Array and Hash
 
33
# @since 3.5 for other enumerables, and support for blocks with 2 parameters
 
34
# @note requires `parser = future`
 
35
#
 
36
Puppet::Functions.create_function(:map) do
 
37
  dispatch :map_Hash_2 do
 
38
    param 'Hash[Any, Any]', :hash
 
39
    required_block_param 'Callable[2,2]', :block
 
40
  end
 
41
 
 
42
  dispatch :map_Hash_1 do
 
43
    param 'Hash[Any, Any]', :hash
 
44
    required_block_param 'Callable[1,1]', :block
 
45
  end
 
46
 
 
47
  dispatch :map_Enumerable_2 do
 
48
    param 'Any', :enumerable
 
49
    required_block_param 'Callable[2,2]', :block
 
50
  end
 
51
 
 
52
  dispatch :map_Enumerable_1 do
 
53
    param 'Any', :enumerable
 
54
    required_block_param 'Callable[1,1]', :block
 
55
  end
 
56
 
 
57
  def map_Hash_1(hash, pblock)
 
58
    hash.map {|x, y| pblock.call(nil, [x, y]) }
 
59
  end
 
60
 
 
61
  def map_Hash_2(hash, pblock)
 
62
      hash.map {|x, y| pblock.call(nil, x, y) }
 
63
  end
 
64
 
 
65
  def map_Enumerable_1(enumerable, pblock)
 
66
    result = []
 
67
    index = 0
 
68
    enum = asserted_enumerable(enumerable)
 
69
    begin
 
70
      loop { result << pblock.call(nil, enum.next) }
 
71
    rescue StopIteration
 
72
    end
 
73
    result
 
74
  end
 
75
 
 
76
  def map_Enumerable_2(enumerable, pblock)
 
77
    result = []
 
78
    index = 0
 
79
    enum = asserted_enumerable(enumerable)
 
80
    begin
 
81
      loop do
 
82
        result << pblock.call(nil, index, enum.next)
 
83
        index = index +1
 
84
      end
 
85
    rescue StopIteration
 
86
    end
 
87
    result
 
88
  end
 
89
 
 
90
  def asserted_enumerable(obj)
 
91
    unless enum = Puppet::Pops::Types::Enumeration.enumerator(obj)
 
92
      raise ArgumentError, ("#{self.class.name}(): wrong argument type (#{obj.class}; must be something enumerable.")
 
93
    end
 
94
    enum
 
95
  end
 
96
 
 
97
end