Sha256: 6db92bcf07712e26db71e606ff0640c4ed3022d9178f46b476b0dc6c568a6e32

Contents?: true

Size: 1.56 KB

Versions: 54

Compression:

Stored size: 1.56 KB

Contents

Puppet::Parser::Functions::newfunction(
  :slice,
  :type => :rvalue,
  :arity => -3,
  :doc => <<-DOC
This function takes two mandatory arguments: the first should be an array or hash, and the second specifies
the number of elements to include in each slice.

When the first argument is a hash, each key value pair is counted as one. For example, a slice size of 2 will produce
an array of two arrays with key, and value.

    $a.slice(2) |$entry|          { notice "first ${$entry[0]}, second ${$entry[1]}" }
    $a.slice(2) |$first, $second| { notice "first ${first}, second ${second}" }

The function produces a concatenated result of the slices.

    slice([1,2,3,4,5,6], 2) # produces [[1,2], [3,4], [5,6]]
    slice(Integer[1,6], 2)  # produces [[1,2], [3,4], [5,6]]
    slice(4,2)              # produces [[0,1], [2,3]]
    slice('hello',2)        # produces [[h, e], [l, l], [o]]

You can also optionally pass a lambda to slice.

    $a.slice($n) |$x| { ... }
    slice($a) |$x| { ... }

The lambda should have either one parameter (receiving an array with the slice), or the same number
of parameters as specified by the slice size (each parameter receiving its part of the slice).
If there are fewer remaining elements than the slice size for the last slice, it will contain the remaining
elements. If the lambda has multiple parameters, excess parameters are set to undef for an array, or
to empty arrays for a hash.

    $a.slice(2) |$first, $second| { ... }

- Since 4.0.0
DOC
) do |args|
  function_fail(["slice() is only available when parser/evaluator future is in effect"])
end

Version data entries

54 entries across 54 versions & 2 rubygems

Version Path
puppet-retrospec-1.5.0 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/slice.rb
puppet-retrospec-1.4.1 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/slice.rb
puppet-retrospec-1.4.0 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/slice.rb
puppet-retrospec-1.3.2 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/slice.rb
puppet-retrospec-1.3.1 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/slice.rb
puppet-retrospec-1.3.0 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/slice.rb
puppet-4.7.1 lib/puppet/parser/functions/slice.rb
puppet-4.7.1-x86-mingw32 lib/puppet/parser/functions/slice.rb
puppet-4.7.1-x64-mingw32 lib/puppet/parser/functions/slice.rb
puppet-4.7.1-universal-darwin lib/puppet/parser/functions/slice.rb
puppet-retrospec-1.2.1 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/slice.rb
puppet-retrospec-1.2.0 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/slice.rb
puppet-4.7.0 lib/puppet/parser/functions/slice.rb
puppet-4.7.0-x86-mingw32 lib/puppet/parser/functions/slice.rb
puppet-4.7.0-x64-mingw32 lib/puppet/parser/functions/slice.rb
puppet-4.7.0-universal-darwin lib/puppet/parser/functions/slice.rb
puppet-4.6.2 lib/puppet/parser/functions/slice.rb
puppet-4.6.2-x86-mingw32 lib/puppet/parser/functions/slice.rb
puppet-4.6.2-x64-mingw32 lib/puppet/parser/functions/slice.rb
puppet-4.6.2-universal-darwin lib/puppet/parser/functions/slice.rb