Seed7 - The extensible programming language
Seed7 FAQ Manual Screenshots Examples Libraries Algorithms Download Links
Examples echo args file in string simple clock count words subtype map function generic func declare stmt template operator 3-way if
Examples
Map function
 previous   up   next 

A map function takes an array, a variable and an expression and returns an array where the expression was applied to every array element. E.g.: When 'x' is an integer variable

doMap([](1, 2, 4, 6, 10, 12, 16), x, x + 1)

will add 1 to every element in the array. Therefore it will return

[](2, 3, 5, 7, 11, 13, 17)

The variable 'x' is used as input parameter for the expression 'x + 1'. The function 'doMap' is not predefined, but it can be defined with:

const func array integer: doMap (in array integer: anArray,
    inout integer: aVariable, ref func integer: anExpression) is func

  result
    var array integer: mappedArray is 0 times 0;
  begin
    for aVariable range anArray do
      mappedArray &:= anExpression;
    end for;
  end func;

Note that the call of 'doMap' works without lambda expression. Instead 'doMap' is defined with the call-by-name parameter 'anExpression'. The parameter 'aVariable' is the input parameter for 'anExpression'.


 previous   up   next