Using Map in Pas2JS

Top 

Example code : Mapping an array of numbers to an array of square roots / squares

var

  points : array of Float = [440098125100];

begin

  var roots := points.Map(Sqrt);

  for var Index := Low(roots) to High(roots) do

      WriteLn(roots[Index]);

 

  var squares := points.Map(Sqr);

  for var Index := Low(squares) to High(squares) do

      WriteLn(squares[Index]);

Result is:

2

20

3

9

5

10

 

16

160000

81

6561

625

10000

---------------------------------

 

The following code takes an array of numbers and creates a new array containing the double of the numbers in the first array. In the second, a new array is created with the first letter.

 

 

 

Example code : Mapping an array of numbers to an array of doubles

var

  points : array of Float = [440098125100];

  palav  : array of String = ("Hello World","Galo","Cruzeiro");

Begin

  var doubles := points.map(function(num: Float) : Float

                              Begin

                                num := (num * 2);

                                result := num;

                             end );

  for var Index := Low(doubles) to High(doubles) do

      WriteLn(doubles[Index]);

 

 

  var myWord := palav.Map(function(x: String) : String

                  Begin

                    result := CharAt(x,1);

                  end );

  for var Index := Low(myWord) to High(myWord) do

     WriteLn(myWord[Index]);

Result is:

8

800

18

162

50

200

 

H

G

C

---------------------------------

JS output:

function W3Button2Click(Self, Sender$5) {

      var points = [];

      var points = [4,400,9,81,25,100].slice();

      var palav = [];

      var palav = ["Hello World","Galo","Cruzeiro"].slice();

      var roots = [];

      var Index = 0;

      var squares = [];

      var Index$1 = 0;

      var doubles = [];

      var Index$2 = 0;

      var myWord = [];

      var Index$3 = 0;

      var a$54 = 0,

          a$55 = 0,

          a$56 = 0,

          a$57 = "";

      roots = points.map(Sqrt);

      var $temp24;

      for(Index = 0,$temp24 = roots.length;Index<$temp24;Index++) {

         WriteLn($DIdxR(roots,Index,""));

      }

      squares = points.map(Sqr$_Float_);

      var $temp25;

      for(Index$1 = 0,$temp25 = squares.length;Index$1<$temp25;Index$1++) {

         WriteLn($DIdxR(squares,Index$1,""));

      }

      doubles = points.map(function (num) {

         var Result = 0;

         num*=2;

         Result = num;

         return Result

      });

      var $temp26;

      for(Index$2 = 0,$temp26 = doubles.length;Index$2<$temp26;Index$2++) {

         WriteLn($DIdxR(doubles,Index$2,""));

      }

      myWord = palav.map(function (x$15) {

         return CharAt(x$15,1);

      });

      var $temp27;

      for(Index$3 = 0,$temp27 = myWord.length;Index$3<$temp27;Index$3++) {

         WriteLn($DIdxR(myWord,Index$3,""));

      }

   }