Apply a callback to an array

Revision as of 22:48, 22 August 2007 by rosettacode>TucRup

yashica fx 3 super pacioli obbsesion massaggio anticellulite inchieste commerciali e industriali videos fotos dibujos blond record guida di maya bisex milano estratti di radice direttiva 91 689 cee suore pisa teatro terapia scheda madre asus scsi rio - carbon 5gb test di mutagenesi birch society www giles it kelly chen treviso singole yu gi oh disegni da colorare sandy pelada multifunzione laser colori scanner fibro- sodimm ddr pc2100 non-ecc troie lecce vittoria in provincia di ragusa jvc dr mh 50 se free foto sex innovazione gov it adsl modem internet gateway router sharp aquos 37 fabrizio gps tom tom per nokia 6630 registratore cassette lampadine risparmio energetico codici carte servizi omnitel blue car wash il cavaliere della valle solitaria vasco rssi www pesca it auto noleggio arles grego megama pietramelara googlre trucchi crusader italiano elena rosenkova tv color lcd 17 amstrad lcd 42 fotocopiatrici e fax fd usb mercedes e 230 avantgarde elisa foto shitt caleffi minestra di pisello e carciofo spartito gianna nannini sei anima togni luna exilim ex s 3 casio dubayy space truckers www buongusto com salome pietra ollare gioiello perla modelli navali in legno mitsubishi pajero sport carrello acciaio la madonnina dai riccioli d oro fasi di venere gabbia per uccelli chaise longue le corbusier tv acer 20 nissan micra km 0 volvo 240 super polar spartiti masini giampiero galeazzi quello che volevi lultimo mercenario concorsi universita rimini una volta il west nona sinfonie lupo auto foto trans bisex drago stea freek a leek palermo new york biglietti aerei ballad pour adeline gioco porno i proibiti amori di tokyo sagarmatha un pugno di polvere let s get it started b e p ospedale valduce orecchino piercing tetta enormi notizie sulle foto radeon 9600 xt 256 tancredi rossini ntsc video ef 70-200mm professional care 7500 sfondi citta cuffia dj stanton suonerie polifoniche da comporre teatro degli arcimboldi gigi e andrea gif animate hentai lunanera google i videos porno de michell vieth disegni da colorare sulla befana er cavaliere nero proietti dual processor workstation decoder wireless programmatore as 400 (regione lombardia - bergamo provincia) premier ds-5330 christian dior fahrenheit dieta 1200 caloria tania cagnotto nuda canon ip1600 cartuccia nero vedere fica wwwauto nvidia geforce 6800 batmam laura pausini strani amori come fare allungare il pene gina ryder assistenza inno hit arezzo la notte della taranta bouwling www posiciones com co decespugliatore 4 tempi agatha christie collection. vol. 3 strategia marketing tha reason hoobastank toyota corolla 1.4 16v 3p. sol la nasa jesse imovane kennedy et moi vasco rossi buoni e cattivi dustin hoffman gig d agostino liz hurley dante alighieri a spasso con la morte dama su scacchiera gai mattiolo - thats amore lui vorbei christina dsc v1 sony steimberg cubase vst codici sbloco nec e228 polize assicurative william basinski verona lumache la figlia del reggimento giurisprudenza appunti camera housing windows 2003 srv std climatizzatore portatile split km0 renault auto km 0 riduttore seggiolino bimbo lola la fattoria voli aerei new york stranded philips voice tracer 7880 musicanova la vita segreta di ian fleming traduzione testo she will be loved il box di jack ryan da vinci leonardo lacie 160gb esterno miracolo a milano topless vacanza mare delonghi 46 oregon scientific basic googl luly bosa exilim pro casio turismo sardegna luesi immigrationexpert com big monei la signora di montecarlo designjet 70 virtua striker 2 ritornerai bruno lauzi ochii tai www angela melillo it microsoft office professional edition diras que estoy loco daniella rush caratteristiche tecniche di telecamere veronica maya pda compact flash konica 1216 toner batteria elettronica con pad corvi

Task
Apply a callback to an array
You are encouraged to solve this task according to the task description, using any language you may know.

Ada

Tested With:

with Ada.Text_Io;
with Ada.Integer_text_IO;

procedure Call_Back_Example is
   -- Purpose: Apply a callback to an array
   -- Output: Prints the squares of an integer array to the console
  
   -- Define the callback procedure
   procedure Display(Location : Positive; Value : Integer) is
   begin
      Ada.Text_Io.Put("array(");
      Ada.Integer_Text_Io.Put(Item => Location, Width => 1);
      Ada.Text_Io.Put(") = ");
      Ada.Integer_Text_Io.Put(Item => Value * Value, Width => 1);
      Ada.Text_Io.New_Line;
   end Display;
  
   -- Define an access type matching the signature of the callback procedure
   type Call_Back_Access is access procedure(L : Positive; V : Integer);
  
   -- Define an unconstrained array type
   type Value_Array is array(Positive range <>) of Integer;
  
   -- Define the procedure performing the callback
   procedure Map(Values : Value_Array; Worker : Call_Back_Access) is
   begin
      for I in Values'range loop
         Worker(I, Values(I));
      end loop;
   end Map;
  
   -- Define and initialize the actual array
   Sample : Value_Array := (5,4,3,2,1);
  
begin
   Map(Sample, Display'access);   
end Call_Back_Example;

C

Tested With:

  • GCC 3.3.6
    • i686-pc-linux-gnu
  • GCC 3.4.6
    • i686-pc-linux-gnu
  • GCC 4.0.3
    • i686-pc-linux-gnu
  • GCC 4.1.1
    • i686-pc-linux-gnu
    • powerpc-unknown-linux-gnu
  • TCC 0.9.23
    • i686-pc-linux-gnu
  • ICC 9.1
    • i686-pc-linux-gnu

callback.h

 #ifndef __CALLBACK_H
 #define __CALLBACK_H
 /*
  * By declaring the function in a separate file, we allow
  * it to be used by other source files.
  *
  * It also stops ICC from complaining.
  *
  * If you don't want to use it outside of callback.c, this
  * file can be removed, provided the static keyword is prepended
  * to the definition.
  */
 void map(int* array, int len, void(*callback)(int,int));
 #endif

callback.c

 #include <stdio.h>
 #include "callback.h"
 /*
  * We don't need this function outside of this file, so
  * we declare it static.
  */
 static void callbackFunction(int location, int value)
 {
   printf("array[%d] = %d\n", location, value);
 } 
 void map(int* array, int len, void(*callback)(int,int))
 {
   int i;
   for(i = 0; i < len; i  )
   {
      callback(i, array[i]);
   }
 } 
 int main()
 {
   int array[] = { 1, 2, 3, 4 };
   map(array, 4, callbackFunction);
   return 0;
 }

Output

 array[0] = 1
 array[1] = 2
 array[2] = 3
 array[3] = 4

C#

Platform: .NET

Language Version: 2.0

Compiler: Visual C# 2005

using System; 

static class Program
{
  // Purpose: Apply a callback (or anonymous method) to an Array
  // Output: Prints the squares of an int array to the console.
  // Compiler: Visual Studio 2005
  // Framework: .net 2
   
  [STAThread]
  public static void Main() 
  {
    int[] intArray = { 1, 2, 3, 4, 5 };

    // Using a callback,
    Console.WriteLine("Printing squares using a callback:");
    Array.ForEach<int>(intArray, PrintSquare);

    // or using an anonymous method:
    Console.WriteLine("Printing squares using an anonymous method:");
    Array.ForEach<int>
    (
      intArray,
      delegate(int value) 
      {
        Console.WriteLine(value * value);    
      });
  }

  public static void PrintSquare(int value) 
  { 
    Console.WriteLine(value * value);
  }
}

C

Compiler: GNU Compiler Collection 4.1.1

Using c-style array

#include <iostream> //cout for printing
#include <algorithm> //for_each defined here
//create the function (print the square)
void print_square(int i) {
  std::cout << i*i << " ";
}
int main() {
  //create the array
  int ary[]={1,2,3,4,5};
  //stl for_each
  std::for_each(ary,ary 5,print_square);
  return 0;
}
//prints 1 4 9 16 25

Using std::vector

#include <iostream> //cout for printing
#include <algorithm> //for_each defined here
#include <vector> //stl vector class
//create the function (print the square)
void print_square(int i) {
  std::cout << i*i << " ";
}
int main() {
  //create the array
  std::vector<int> ary;
  ary.push_back(1);
  ary.push_back(2);
  ary.push_back(3);
  ary.push_back(4);
  ary.push_back(5);
  //stl for_each
  std::for_each(ary.begin(),ary.end(),print_square);
  return 0;
}
//prints 1 4 9 16 25

More tricky with binary function

#include <iostream> //cout for printing
#include <algorithm> //for_each defined here
#include <vector> //stl vector class
#include <functional> //bind and ptr_fun
//create a binary function (print any two arguments together)
template<class type1,class type2>
void print_juxtaposed(type1 x, type2 y) {
  std::cout << x << y;
}
int main() {
  //create the array
  std::vector<int> ary;
  ary.push_back(1);
  ary.push_back(2);
  ary.push_back(3);
  ary.push_back(4);
  ary.push_back(5);
  //stl for_each, using binder and adaptable unary function
  std::for_each(ary.begin(),ary.end(),std::bind2nd(std::ptr_fun(print_juxtaposed<int,std::string>),"x "));
  return 0;
}
//prints 1x 2x 3x 4x 5x

Using Boost.Lambda

 using namespace std;
 using namespace boost::lambda;
 vector<int> ary(10);
 int i = 0;
 for_each(ary.begin(), ary.end(), _1 =   var(i)); // init array
 transform(ary.begin(), ary.end(), ostream_iterator<int>(cout, " "), _1 * _1); // square and output

Clean

Define a function and an initial (unboxed) array.

square x = x * x

values :: {#Int}
values = {x \\ x <- [1 .. 10]}

One can easily define a map for arrays, which is overloaded and works for all kinds of arrays (lazy, strict, unboxed).

mapArray f array = {f x \\ x <-: array}

Apply the function to the initial array (using a comprehension) and print result.

Start :: {#Int}
Start = mapArray square values

Common Lisp

Imperative: print 1, 2, 3, 4 and 5:

 (map nil #'print #(1 2 3 4 5))

Functional: collect squares into new vector that is returned:

 (defun square (x) (* x x))
 (map 'vector #'square #(1 2 3 4 5))

Destructive, like the Javascript example; add 1 to every slot of vector *a*:

 (defvar *a* (vector 1 2 3))
 (map-into *a* #'1  *a*)

E

def array := [1,2,3,4,5]
def square(value) { 
    return value * value
}

Example of builtin iteration:

def callback(index, value) { 
    println(`Item $index is $value.`)
}
array.iterate(callback)

There is no builtin map function yet. the following is one of the ways one could be implemented, returning a plain list (which is usually an array in implementation).

def map(func, collection) {
    def output := [].diverge()
    for item in collection {
        output.push(func(item))
    }
    return output.snapshot()
}
println(map(square, array))

Forth

This is a word that will call a given function on each cell in an array.

: map ( addr n fn -- )
   -rot cells bounds do  i @ over execute i !  cell  loop ;

Example usage:

create data 1 , 2 , 3 , 4 , 5 ,
data 5 ' 1  map  \ adds one to each element of data

Fortran

      program test
C
C--   Declare array:
      integer a(5)
C
C--   Fill it with Data
      data a /45,22,67,87,98/
C
C--   Do something with all elements (in this case: print their squares)
      do i=1,5
        print *,a(i)*a(i)
      end do
C
      end


FP

Interpreter : "fp"

 {square * . [id, id]}