Class: AmberVM::Functions::Map

Inherits:
Function show all
Defined in:
lib/amber/functions/list/map.rb

Overview

The map function is used to execute a certain task for every elment of a List, the results of the map function are used to replace the original element.

The map function passes two arguments to the mapping funciton, first the element of the list to be mapped and second the position of this element.

It eppects twp parameters, first the List and second the mapping function, which should resond to #call

Class Method Summary collapse

Methods inherited from Function

call, data_type, execargs, method_missing

Methods included from Plugin

#helper, #included, #plugin_host, #plugin_id, #register_for

Class Method Details

.execute(params, env) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/amber/functions/list/map.rb', line 38

def Map.execute params, env
  if params.length == 2
    block = params[1]
    i = 0
    AmberVM::Classes::List.new(params[0].map do |e|
      e = block.call([e, AmberVM::Classes[:number].new(i)], env)
      i += 1
      e # needed to assure that the right value is returned
    end,params[0].sepperator)
  else
    AmberVM::Classes[:error].new(1,"FUNCTION (#{self.class.to_s}) EXPECTS 2 ARGUMENTS BUT GOT #{params.length}")
  end
end

.signatureObject



52
53
54
# File 'lib/amber/functions/list/map.rb', line 52

def Map.signature
  [AmberVM::Classes::List, AmberVM::Classes::Block]
end