Apply a callback to an array: Difference between revisions

m
<code>, fixed use of reserved identifier in C code, some minor formatting improvements
No edit summary
m (<code>, fixed use of reserved identifier in C code, some minor formatting improvements)
Line 2:
 
=={{header|ActionScript}}==
<code actionscript>
package
{
Line 22:
}
}
</code>
</actionscript>
 
=={{header|Ada}}==
{{works with|GNAT|GPL 2005}}
<code ada>
<ada>with Ada.Text_Io;
with Ada.Text_Io;
with Ada.Integer_text_IO;
Line 62 ⟶ 63:
begin
Map(Sample, Display'access);
end Call_Back_Example;</ada>
</code>
 
=={{header|ALGOL 68}}==
Line 95 ⟶ 97:
{{works with|ICC|9.1}}
===callback.h===
<code c>
#ifndef CALLBACK_H
#define CALLBACK_H
 
/*
#ifndef __CALLBACK_H
* By declaring the function in a separate file, we allow
#define __CALLBACK_H
* it to be used by other source files.
/*
*
* By declaring the function in a separate file, we allow
* It also stops ICC from complaining.
* it to be used by other source files.
*
* If you don't want to use it outside of callback.c, this
* It also stops ICC from complaining.
* file can be removed, provided the static keyword is prepended
*
* to the definition.
* If you don't want to use it outside of callback.c, this
*/
* file can be removed, provided the static keyword is prepended
void map(int* array, int len, void(*callback)(int,int));
* to the definition.
 
*/
#endif
void map(int* array, int len, void(*callback)(int,int));
</code>
#endif
 
===callback.c===
<code>
#include <stdio.h>
#include "callback.h"
 
/*
#include <stdio.h>
* We don't need this function outside of this file, so
#include "callback.h"
* we declare it static.
/*
*/
* We don't need this function outside of this file, so
static void callbackFunction(int location, int value)
* we declare it static.
{
*/
staticprintf("array[%d] void= callbackFunction(int%d\n", location, int value);
}
 
void map(int* array, int len, void(*callback)(int,int))
{
int i;
for(i = 0; i < len; i++)
{
printf callback("i, array[%di] = %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;
}
}
 
int main()
{
int array[] = { 1, 2, 3, 4 };
map(array, 4, callbackFunction);
return 0;
}
</code>
 
===Output===
Line 194 ⟶ 203:
{{works with|g++|4.1.1}}
===C-Style Array===
<code cpp>
#include <iostream> //cout for printing
#include <algorithm> //for_each defined here
 
//create the function (print the square)
#include <iostream> //cout for printing
void print_square(int i) {
#include <algorithm> //for_each defined here
std::cout << i*i << " ";
//create the function (print the square)
}
void print_square(int i) {
 
std::cout << i*i << " ";
int main() {
}
//create the array
int main() {
int ary[]={1,2,3,4,5};
//create the array
//stl for_each
int ary[]={1,2,3,4,5};
//stl std::for_each(ary,ary+5,print_square);
return 0;
std::for_each(ary,ary+5,print_square);
}
return 0;
//prints 1 4 9 16 25
}
</code>
//prints 1 4 9 16 25
 
===std::vector===
{{libheader|STL}}
<code cpp>
#include <iostream> // cout for printing
#include <algorithm> // for_each defined here
#include <vector> // stl vector class
 
// create the function (print the square)
#include <iostream> //cout for printing
void print_square(int i) {
#include <algorithm> //for_each defined here
std::cout << i*i << " ";
#include <vector> //stl vector class
}
//create the function (print the square)
 
void print_square(int i) {
int main() {
std::cout << i*i << " ";
// create the array
}
std::vector<int> ary;
int main() {
ary.push_back(1);
//create the array
ary.push_back(2);
std::vector<int> ary;
ary.push_back(13);
ary.push_back(24);
ary.push_back(35);
// stl for_each
ary.push_back(4);
std::for_each(ary.begin(),ary.end(),print_square);
ary.push_back(5);
return 0;
//stl for_each
}
std::for_each(ary.begin(),ary.end(),print_square);
//prints 1 4 9 16 25
return 0;
</code>
}
//prints 1 4 9 16 25
 
More tricky with binary function
<code>
#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)
#include <iostream> //cout for printing
template<class type1,class type2>
#include <algorithm> //for_each defined here
void print_juxtaposed(type1 x, type2 y) {
#include <vector> //stl vector class
std::cout << x << y;
#include <functional> //bind and ptr_fun
}
//create a binary function (print any two arguments together)
 
template<class type1,class type2>
int main() {
void print_juxtaposed(type1 x, type2 y) {
// create the array
std::cout << x << y;
std::vector<int> ary;
}
ary.push_back(1);
int main() {
ary.push_back(2);
//create the array
ary.push_back(3);
std::vector<int> ary;
ary.push_back(14);
ary.push_back(25);
// stl for_each, using binder and adaptable unary function
ary.push_back(3);
std::for_each(ary.begin(),ary.end(),std::bind2nd(std::ptr_fun(print_juxtaposed<int,std::string>),"x "));
ary.push_back(4);
return 0;
ary.push_back(5);
}
//stl for_each, using binder and adaptable unary function
//prints 1x 2x 3x 4x 5x
std::for_each(ary.begin(),ary.end(),std::bind2nd(std::ptr_fun(print_juxtaposed<int,std::string>),"x "));
</code>
return 0;
}
//prints 1x 2x 3x 4x 5x
 
===Boost.Lambda===
{{libheader|Boost}}
<code cpp>
 
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
</code>
 
=={{header|Clean}}==
Line 291 ⟶ 310:
Imperative: print 1, 2, 3, 4 and 5:
 
<code lisp>
(map nil #'print #(1 2 3 4 5))
(map nil #'print #(1 2 3 4 5))
</code>
 
Functional: collect squares into new vector that is returned:
 
<code lisp>
(defun square (x) (* x x))
(mapdefun 'vector #'square #(1x) 2 3(* 4x 5x))
(map 'vector #'square #(1 2 3 4 5))
</code>
 
Destructive, like the Javascript example; add 1 to every slot of vector *a*:
 
<code lisp>
(defvar *a* (vector 1 2 3))
(map-intodefvar *a* #'(vector 1+ *a*2 3))
(map-into *a* #'1+ *a*)
</code>
 
=={{header|Clojure}}==
Line 315 ⟶ 340:
 
=={{header|D}}==
<code d>
<d>U[] map(T, U)(T[] array, U delegate(T) dg) {
U[] map(T, U)(T[] array, U delegate(T) dg) {
auto result = new U[array.length];
Line 328 ⟶ 354:
[1, 2, 3, 4, 5].map( (int i) { return i+5; } )
);
}
}</d>
</code>
Using std.algorithm:
<code d>
<d>writefln(map!("a + 5")([1, 2, 3, 4, 5]));</d>
writefln(map!("a + 5")([1, 2, 3, 4, 5]));
</code>
 
=={{header|E}}==
Line 373 ⟶ 402:
 
{{Works with |Fortran|ISO 95 and later}}
<code fortran>
module arrCallback
module arrCallback
contains
contains
elemental function cube( x )
elemental function cube( x )
implicit none
implicit real :: cubenone
real, intent(in) :: xcube
real, cubeintent(in) = x * x *:: x
end function cube = x * x * x
end function cube
end module arrCallback
end module arrCallback
</code>
 
<code fortran>
program testAC
program testAC
use arrCallback
use implicit nonearrCallback
implicit none
integer :: i, j
real, dimension(3,4)integer :: bi, &j
real, dimension(3,4) :: b, &
a = reshape( (/ ((10 * i + j, i = 1, 3), j = 1, 4) /), (/ 3,4 /) )
a = reshape( (/ ((10 * i + j, i = 1, 3), j = 1, 4) /), (/ 3,4 /) )
do i = 1, 3
do i = write(*1,*) a(i,:)3
end do write(*,*) a(i,:)
end do
b = cube( a ) ! Applies CUBE to every member of a,
b = cube( a ) ! andApplies storesCUBE eachto resultevery in the equivalent elementmember of ba,
! and stores each result in the equivalent element of b
do i = 1, 3
do i = write(*1,*) b(i,:)3
end do write(*,*) b(i,:)
end do
end program testAC
end program testAC
</code>
 
{{Works with|ANSI FORTRAN| 77 (with MIL-STD-1753 structured DO) and later}}
<!-- NOTE TO EDITORS OF THIS EXAMPLE:
program test
The following contains an invisible unicode character (U+FEFF) at the
C
start of the first FORTRAN code line (i.e. "program test") in order to work
C-- Declare array:
around a bug in code tags, which would break correct FORTRAN 77 column alignment.
integer a(5)
As long as the code tag problem isn't fixed, please make sure that you don't
C
accidentally delete that invisible character! -->
C-- Fill it with Data
<code fortran>
data a /45,22,67,87,98/
 program test
C
C
C-- Do something with all elements (in this case: print their squares)
C-- Declare array:
do i=1,5
integer print *,a(i)*a(i5)
C
end do
C-- Fill it with Data
C
data enda /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
</code>
 
=={{header|FP}}==
Line 433 ⟶ 474:
===List===
{{works with|GHC}}
<code haskell>
let square x = x*x
let square x = x*x
let values = [1..10]
let values = [1..10]
map square values
map square values
</code>
 
Using list comprehension to generate a list of the squared values
<code haskell>
[square x | x <- values]
[square x | x <- values]
</code>
 
Using function composition to create a function that will print the squares of a list
<code haskell>
let printSquares = putStr.unlines.map (show.square)
let printSquares values= putStr.unlines.map (show.square)
printSquares values
</code>
 
===Array===
{{works with|GHC}}
<code haskell>
import Data.Array.IArray
import Data.Array.IArray
let square x = x*x
let square x = x*x
let values = array (1,10) [(i,i)|i <- [1..10]] :: Array Int Int
let values = array (1,10) [(i,i)|i <- [1..10]] :: Array Int Int
amap square values
amap square values
</code>
 
=={{header|Icon}}==
Line 492 ⟶ 541:
So if you want to perform an action (which doesn't return anything) on an array of int's:
 
<code java>
<java>interface IntToVoid {
interface IntToVoid {
void run(int x);
}
Line 502 ⟶ 552:
}
}.run(z);
}
}</java>
</code>
 
Or if you want to perform "map" - return an array of the results of function applications:
 
<code java>
<java>interface IntToInt {
interface IntToInt {
int run(int x);
}
Line 518 ⟶ 570:
}
}.run(myIntArray[i]);
}
}</java>
</code>
 
=={{header|JavaScript}}==
Line 524 ⟶ 577:
Portable technique:
 
<code javascript>
function map(a, func) {
function map(a, func) {
for (var i in a)
for (var a[i] =in func(a[i]);
a[i] = func(a[i]);
}
}
 
var a = [1, 2, 3, 4, 5];
var a = [1, 2, 3, 4, 5];
map(a, function(v) { return v * v; });
map(a, function(v) { return v * v; });
</code>
 
{{libheader|BeyondJS}}
With the [http://w3future.com/html/beyondJS/ BeyondJS] library:
 
<code javascript>
var a = (1).to(10).collect(Math.pow.curry(undefined,2));
var a = (1).to(10).collect(Math.pow.curry(undefined,2));
</code>
 
With Firefox 2.0:
 
<code javascript>
function cube(num) {
function cube(num) {
return Math.pow(num, 3);
return Math.pow(num, 3);
}
}
var numbers = [1, 2, 3, 4, 5];
//get results of calling cube on every element
var cubes1 = numbers.map(cube);
//display each result in a separate dialog
cubes1.forEach(alert);
//array comprehension
var cubes2 = [cube(n) for each (n in numbers)];
var cubes3 = [n * n * n for each (n in numbers)];
 
var numbers = [1, 2, 3, 4, 5];
{{libheader|Functional}}
Functional.map('x*x*x', [1,2,3,4,5])
 
//get results of calling cube on every element
var cubes1 = numbers.map(cube);
 
//display each result in a separate dialog
cubes1.forEach(alert);
 
//array comprehension
var cubes2 = [cube(n) for each (n in numbers)];
var cubes3 = [n * n * n for each (n in numbers)];
</code>
 
{{libheader|Functional}}
<code javascript>
Functional.map('x*x*x', [1,2,3,4,5])
</code>
=={{header|Logo}}==
to square :x
Line 593 ⟶ 653:
This function is part of the standard library:
 
<code ocaml>
Array.map
Array.map
</code>
 
Usage example:
<code ocaml>
 
let square x = x * x;;
let values = Array.init 10 ((+) 1);;
Array.map square values;;
</code>
 
=={{header|Oz}}==
Line 629 ⟶ 692:
 
{Application.exit 0}
end</pre>
</pre>
=={{header|Perl}}==
<code perl>
# create array
my @a = (1, 2, 3, 4, 5);
 
# create arraycallback function
sub mycallback {
my @a = (1, 2, 3, 4, 5);
return 2 * shift;
}
# create callback function
 
sub mycallback {
# use array indexing
return 2 * shift;
my $i;
}
for ($i = 0; $i < scalar @a; $i++) {
print "mycallback($a[$i]) = ", mycallback($a[$i]), "\n";
# use array indexing
}
my $i;
 
for ($i = 0; $i < scalar @a; $i++) {
# using foreach
print "mycallback($a[$i]) = ", mycallback($a[$i]), "\n";
foreach my $x (@a) {
}
print "mycallback($x) = ", mycallback($x), "\n";
}
# using foreach
 
foreach my $x (@a) {
# using map (useful for transforming an array)
print "mycallback($x) = ", mycallback($x), "\n";
my @b = map mycallback($_), @a; # @b is now (2, 4, 6, 8, 10)
}
 
# and the same using an anonymous function
# using map (useful for transforming an array)
my @bc = map mycallback({ $_), * 2 } @a; # @bc is now (2, 4, 6, 8, 10)
 
# use a callback stored in a variable
# and the same using an anonymous function
my $func = \&mycallback;
my @c = map { $_ * 2 } @a; # @c is now (2, 4, 6, 8, 10)
my @d = map $func->($_), @a; # @d is now (2, 4, 6, 8, 10)
</code>
# use a callback stored in a variable
my $func = \&mycallback;
my @d = map $func->($_), @a; # @d is now (2, 4, 6, 8, 10)
 
=={{header|PHP}}==
<code php>
function cube($n)
{
return($n * $n * $n);
}
 
$a = array(1, 2, 3, 4, 5);
function cube($n)
$b = array_map("cube", $a);
{
print_r($b);
return($n * $n * $n);
</code>
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
 
=={{header|PL/SQL}}==
{{works with|Oracle}}
<code plsql>
set serveroutput on
set serveroutput on
declare
declare
type myarray is table of number index by binary_integer;
type myarray is table of number index by binary_integer;
x myarray;
x i pls_integermyarray;
i pls_integer;
begin
begin
-- populate array
-- populate for i in 1..5 looparray
for i in 1..5 x(i) := i;loop
end loop x(i) := i;
end i :=0loop;
i :=0;
 
-- square array
-- square looparray
i := i + 1;loop
i := begini + 1;
x(i) := x(i)*x(i);begin
x(i) := dbms_output.put_line(x(i)*x(i);
exception dbms_output.put_line(x(i));
exception when no_data_found then exit;
end when no_data_found then exit;
end loop end;
end loop;
 
end;
end;
/
/
</code>
 
=={{header|Pop11}}==
Line 716 ⟶ 784:
 
=={{header|Python}}==
<code python>
def square(n):
return n * n
Line 735 ⟶ 803:
import itertools
isquares2 = itertools.imap(square, numbers) # iterator, lazy
</pythoncode>
To print squares of integers in the range from 0 to 9, type:
<code python>
<python>print " ".join(str(n * n) for n in range(10))</python>
print " ".join(str(n * n) for n in range(10))
</code>
Or:
<code python>
<python>print " ".join(map(str, map(square, range(10))))</python>
print " ".join(map(str, map(square, range(10))))
</code>
Result:
<python>0code 1 4 9 16 25 36 49 64 81</python>
0 1 4 9 16 25 36 49 64 81
</code>
 
=={{header|Raven}}==
Line 754 ⟶ 828:
 
=={{header|Ruby}}==
# You could use a traditional "for i in arr" approach like below:
<code ruby>
for i in [1,2,3,4,5] do
for i in [1,2,3,4,5] do
puts i**2
puts i**2
end
end
</code>
 
# Or you could the more preferred ruby way of an iterator (which is borrowed from SmallTalk)
<code ruby>
[1,2,3,4,5].each{ |i| puts i**2 }
[1,2,3,4,5].each{ |i| puts i**2 }
</code>
 
# To create a new array of each value squared
<code ruby>
[1,2,3,4,5].map{ |i| i**2 }
[1,2,3,4,5].map{ |i| i**2 }
</code>
 
=={{header|Scala}}==
Line 790 ⟶ 870:
 
=={{header|Scheme}}==
<code scheme>
(define (square n) (* n n))
(define x #(1square 2n) 3(* 4n 5n))
(define x #(1 2 3 4 5))
(map square (vector->list x))
(map square (vector->list x))
 
</code>
 
A single-line variation
<code scheme>
(map (lambda (n) (* n n)) '(1 2 3 4 5))
(map (lambda (n) (* n n)) '(1 2 3 4 5))
</code>
 
For completeness, the <tt>map</tt> function (which is R5RS standard) can be coded as follows:
<code scheme>
(define (map f L)
(define (ifmap (null?f L)
(if (null? L)
L
(cons (f (car L)) (map f (cdr L)))))
(cons (f (car L)) (map f (cdr L)))))
</code>
 
=={{header|Smalltalk}}==
<code smalltalk>
 
#( 1 2 3 4 5 ) collect: [:n | n * n].
</code>
 
=={{header|Tcl}}==
 
If I wanted to call "<tt>myfunc</tt>" on each element of <tt>dat</tt> and <tt>dat</tt> were a list:
 
<code tcl>
foreach var $dat { myfunc $var }
foreach var $dat { myfunc $var }
</code>
 
if <tt>dat</tt> were an array, however:
 
<code tcl>
foreach name [array names dat] { myfunc $dat($name) }
foreach name [array names dat] { myfunc $dat($name) }
</code>
 
=={{header|Toka}}==
973

edits