Apply a callback to an array: Difference between revisions

Content added Content deleted
No edit summary
Line 1: Line 1:
[http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SherylDiu/noliwu.htm free midi ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=37 paxil online] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=261 tracfone ringtones] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=254 cheap xanax] [http://bugzilla.internet2.edu/attachment.cgi?id=723 alltel ringtones] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/tofi.htm xanax online] [http://www.hollins.edu/ubb/Forum32/HTML/000344.html pharmacy online online] [http://ccfl.unl.edu/twiki/pub/Main/SummersFrank/jicir.htm cheap viagra] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/vojenyx.htm cheap alprazolam] [http://ist.greenville.edu/drupal/files/sotuky.html cheap soma] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/wudytel.htm free sonyericsson ringtones] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/RobertDelise cheap ultram] [http://bugzilla.internet2.edu/attachment.cgi?id=724 cheap alprazolam] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/webid.htm hoodia online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49017 cheap diazepam] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/rifykup.htm cheap albuterol] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=14 soma online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/hebur.htm free qwest ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=756 verizon ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=729 clonazepam online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SherylDiu/kygo.htm free kyocera ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BilyKroft/suvycez.htm cheap soma] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/JustinJonson/jucidul.htm hgh online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/pyro.htm ambien online] [http://grid.unimelb.edu.au/twiki/pub/Main/NoriJohn/bepi.htm free nextel ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000315.html xenical online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/kyvydin.htm norco online] [http://dcc.syr.edu/dforum/message.asp?MessageID=48834 tramadol online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49026 vicodin online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/tidu.htm free verizon ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/jivugyv.htm alltel ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000371.html free music ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=748 soma online] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=52 funny ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=731 funny ringtones] [http://ccfl.unl.edu/twiki/pub/Main/SummersFrank/cogope.htm cialis online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49058 free tracfone ringtones] [http://ccfl.unl.edu/twiki/pub/Main/SummersFrank/solizup.htm tramadol online] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/ArturJonson xanax online] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/duxeru.htm cheap vigrx] [http://dcc.syr.edu/dforum/message.asp?MessageID=49066 free samsung ringtones] [http://ccfl.unl.edu/twiki/pub/Main/SummersFrank/wytyvib.htm buy fioricet] [http://bugzilla.internet2.edu/attachment.cgi?id=741 paxil online] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=40 ambien online] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/tijohyw.htm sprint ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/jetok.htm free mp3 ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=20 fioricet online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BettySmith/pefuwe.htm cheap lipitor] [http://ist.greenville.edu/drupal/files/wohux.html cheap ultracet] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BettySmith/ligi.htm tenuate online] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/cucof.htm buy soma] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/LukeLookin cheap phentermine] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/pinucep.htm cheap vicodin] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/hujox.htm cheap ortho] [http://ist.greenville.edu/drupal/files/wusywyx.html vicodin online] [http://ist.greenville.edu/drupal/files/lunic.html cheap levitra] [http://dcc.syr.edu/dforum/message.asp?MessageID=49024 adipex online] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/vuxinuz.htm free motorola ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=722 adipex online] [http://www.hollins.edu/ubb/Forum32/HTML/000365.html free nextel ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=21 cheap ultram] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/jusify.htm free nokia ringtones] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=262 free funny ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=48922 buy carisoprodol] [http://bugzilla.internet2.edu/attachment.cgi?id=742 phentermine online] [http://www.hollins.edu/ubb/Forum32/HTML/000283.html valium online] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/jowili.htm qwest ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000342.html tenuate online] [http://bugzilla.internet2.edu/attachment.cgi?id=750 sonyericsson ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/licox.htm free motorola ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000319.html cheap cyclobenzaprine] [http://www.hollins.edu/ubb/Forum32/HTML/000295.html norco online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/JustinJonson/tokuny.htm lortab online] [http://www.hollins.edu/ubb/Forum32/HTML/000354.html cheap flexeril] [http://bugzilla.internet2.edu/attachment.cgi?id=752 tracfone ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000285.html cheap carisoprodol] [http://www.hollins.edu/ubb/Forum32/HTML/000299.html paxil] [http://dcc.syr.edu/dforum/message.asp?MessageID=49063 qwest ringtones] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=249 tramadol online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/sywexyc.htm levitra online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/zexuc.htm order didrex] [http://www.hollins.edu/ubb/Forum32/HTML/000380.html free punk ringtones] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/kite.htm ultracet] [http://www.hollins.edu/ubb/Forum32/HTML/000311.html hydrocodone online] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=42 buy xenical] [http://ist.greenville.edu/drupal/files/luho.html nokia ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/rufog.htm sprint ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/JustinJonson/jujope.htm diethylpropion online] [http://www.hollins.edu/ubb/Forum32/HTML/000294.html online adipex] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/roteryh.htm free polyphonic ringtones] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/AndreaHolivel cheap cialis] [http://dcc.syr.edu/dforum/message.asp?MessageID=49016 order ultram] [http://www.hollins.edu/ubb/Forum32/HTML/000336.html cheap clomid] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/hijosij.htm free sagem ringtones] [http://ist.greenville.edu/drupal/files/bigibe.html free nextel ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49027 alprazolam online] [http://www.hollins.edu/ubb/Forum32/HTML/000288.html ultram online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/hicif.htm cheap alprazolam] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/sujy.htm free samsung ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=25 cheap viagra] [http://bugzilla.internet2.edu/attachment.cgi?id=735 free motorola ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/jybuhi.htm cheap viagra] [http://dcc.syr.edu/dforum/message.asp?MessageID=48916 cheap phentermine] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=256 cheap ultram] [http://bugzilla.internet2.edu/attachment.cgi?id=760 cheap diazepam] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/JustinJonson/xyhyj.htm rivotril online] [http://ccfl.unl.edu/twiki/pub/Main/SummersFrank/vuru.htm ativan online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/hykidi.htm order clonazepam] [http://www.hollins.edu/ubb/Forum32/HTML/000281.html cheap tramadol] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=253 buy carisoprodol] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=26 cheap meridia] [http://ist.greenville.edu/drupal/files/dydyse.html valium online] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/rukexo.htm zoloft online] [http://ist.greenville.edu/drupal/files/kuxer.html cyclobenzaprine online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/cybo.htm sonyericsson ringtones] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/nezyz.htm cheap flexeril] [http://ccfl.unl.edu/twiki/pub/Main/SummersFrank/lyfogo.htm online xanax] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/wofo.htm free nokia ringtones] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/sonuhop.htm valium online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/pevesuz.htm cheap wellbutrin] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/rypel.htm sildenafil online] [http://www.hollins.edu/ubb/Forum32/HTML/000339.html zoloft online] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=251 valium online] [http://www.hollins.edu/ubb/Forum32/HTML/000384.html free midi ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000290.html cheap cialis] [http://bugzilla.internet2.edu/attachment.cgi?id=744 free qwest ringtones] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=266 free real ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/kino.htm real ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/belizyl.htm polyphonic ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/dijuxoj.htm tracfone ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000322.html cheap ultracet] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=28 norco online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49072 polyphonic ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=732 levitra online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/woguked.htm ambien online] [http://www.hollins.edu/ubb/Forum32/HTML/000334.html cheap sildenafil] [http://www.hollins.edu/ubb/Forum32/HTML/000333.html prozac online] [http://ist.greenville.edu/drupal/files/rucod.html xanax] [http://www.hollins.edu/ubb/Forum32/HTML/000343.html cheap ortho] [http://ist.greenville.edu/drupal/files/wypy.html funny ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000296.html vicodin online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BilyKroft/koky.htm cheap phentermine] [http://bugzilla.internet2.edu/attachment.cgi?id=740 cheap norco] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/figog.htm lortab online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/kozu.htm hydrocodone online] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=24 cheap ativan] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/godufit.htm buy paxil] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/sevuv.htm mono ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=755 valium] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/sicofyr.htm tramadol online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BettySmith/kuvodoj.htm zoloft online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49034 hydrocodone] [http://dcc.syr.edu/dforum/message.asp?MessageID=49022 viagra online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BettySmith/niwupu.htm but albuterol] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/gedenu.htm rivotril online] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/dyzoko.htm free verizon ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000314.html lorazepam] [http://grid.unimelb.edu.au/twiki/pub/Main/JackLawson/rejyvob.htm cheap viagra] [http://www.hollins.edu/ubb/Forum32/HTML/000325.html order didrex] [http://ccfl.unl.edu/twiki/pub/Main/SummersFrank/kuzypuh.htm phentermine online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/nofelyr.htm free cingular ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000389.html free ericsson ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49077 free sony ericsson ringtones] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=267 free motorola ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/JustinJonson/kuluc.htm buy zanaflex] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/JustinJonson/gekiw.htm order flexeril] [http://www.hollins.edu/ubb/Forum32/HTML/000369.html free samsung ringtones] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=259 free nokia ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=22 diazepam online] [http://www.hollins.edu/ubb/Forum32/HTML/000376.html free sonyericsson ringtones] [http://ist.greenville.edu/drupal/files/devowy.html lorazepam online] [http://ist.greenville.edu/drupal/files/role.html free real ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000351.html but rivotril] [http://www.hollins.edu/ubb/Forum32/HTML/000312.html buy ambien] [http://www.hollins.edu/ubb/Forum32/HTML/000337.html cheap albuterol] [http://dcc.syr.edu/dforum/message.asp?MessageID=49018 cialis online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49035 ambien online] [http://bugzilla.internet2.edu/attachment.cgi?id=746 sagem ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49025 norco online] [http://ist.greenville.edu/drupal/files/puweso.html meridia online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/cibedis.htm cheap nexium] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/zivy.htm free mono ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000352.html cheap hgh] [http://bugzilla.internet2.edu/attachment.cgi?id=754 ultram online] [http://ist.greenville.edu/drupal/files/ruwyze.html order ativan] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/linefej.htm buy wellbutrin] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/rofev.htm free free ringtones] [http://ist.greenville.edu/drupal/files/pobiwi.html diazepam online] [http://bugzilla.internet2.edu/attachment.cgi?id=751 sprint ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000345.html lortab online] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/bopogo.htm buy carisoprodol] [http://dcc.syr.edu/dforum/message.asp?MessageID=49071 free verizon ringtones] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/relu.htm clomid online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/sifus.htm cheap xenical] [http://bugzilla.internet2.edu/attachment.cgi?id=759 cheap xanax] [http://www.hollins.edu/ubb/Forum32/HTML/000360.html free free ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=43 wellbutrin online] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=15 buy carisoprodol] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/DikVolirev free tracfone ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SherylDiu/popuxo.htm sharp ringtones] {{task}}
{{task}}
== [[Ada]] ==
== [[Ada]] ==
[[Category:Ada]]
[[Category:Ada]]
'''Tested With:'''
'''Tested With:'''
* [[Gnat GPL 2005]]
* [[Gnat GPL 2005]]
** Amd-64bit-3500+-WinXP
** Amd-64bit-3500 -WinXP


with Ada.Text_Io;
with Ada.Text_Io;
Line 93: Line 93:
{
{
int i;
int i;
for(i = 0; i < len; i++)
for(i = 0; i < len; i )
{
{
callback(i, array[i]);
callback(i, array[i]);
Line 116: Line 116:
'''Platform:''' [[.NET]]
'''Platform:''' [[.NET]]


'''Language Version:''' 2.0+
'''Language Version:''' 2.0


'''Compiler:''' [[Visual C sharp|Visual C#]] 2005
'''Compiler:''' [[Visual C sharp|Visual C#]] 2005
Line 155: Line 155:
}
}


==[[C plus plus|C++]]==
==[[C plus plus|C ]]==
[[Category:C plus plus]]
[[Category:C plus plus]]
'''Compiler:''' [[GNU Compiler Collection]] 4.1.1
'''Compiler:''' [[GNU Compiler Collection]] 4.1.1
Line 171: Line 171:
int ary[]={1,2,3,4,5};
int ary[]={1,2,3,4,5};
//stl for_each
//stl for_each
std::for_each(ary,ary+5,print_square);
std::for_each(ary,ary 5,print_square);
return 0;
return 0;
}
}
Line 230: Line 230:
vector<int> ary(10);
vector<int> ary(10);
int i = 0;
int i = 0;
for_each(ary.begin(), ary.end(), _1 = ++var(i)); // init array
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
transform(ary.begin(), ary.end(), ostream_iterator<int>(cout, " "), _1 * _1); // square and output


Line 267: Line 267:


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


==[[E]]==
==[[E]]==
Line 301: Line 301:


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


Example usage:
Example usage:


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


==[[Fortran]]==
==[[Fortran]]==
Line 332: Line 332:


{square * . [id, id]}
{square * . [id, id]}
& square: <1,2,3,4,5>

== [[Haskell]] ==
[[Category:Haskell]]
'''Interpreter''' : [[GHC | GHCi]]

'''Compiler''' : [[GHC]]

let square x = x*x
let values = [1..10]
map square values

Using list comprehension to generate a list of the squared values
[square x | x <- values]

Using function composition to create a function that will print the squares of a list
let printSquares = putStr.unlines.map (show.square)
printSquares values


== [[IDL]] ==
[[Category:IDL]]

Hard to come up with an example that isn't completely contrived. IDL doesn't really distinguish between a scalar and an array; thus

b = a^3

will yield a scalar if a is scalar or a vector if a is a vector or an n-dimensional array is a is an n-dimensional array

== [[JavaScript]] ==
[[Category:JavaScript]]

Portable technique:

function map(a, func) {
for (var i in a)
a[i] = func(a[i]);
}
var a = [1, 2, 3, 4, 5];
map(a, function(v) { return v * v; });

With the [http://w3future.com/html/beyondJS/ BeyondJS] library:

var a = (1).to(10).collect(Math.pow.curry(undefined,2));

With Firefox 2.0:

function cube(num) {
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)];

== [[OCaml]] ==
[[Category:OCaml]]
This function is part of the standard library:

Array.map

Usage example:

let square x = x * x;;
let values = Array.init 10 ((+) 1);;
Array.map square values;;

==[[Perl]]==
[[Category:Perl]]

# create array
my @a = (1, 2, 3, 4, 5);

# create callback function
sub mycallback {
return 2 * shift;
}

# use array indexing
my $i;
for ($i = 0; $i < scalar @a; $i++) {
print "mycallback($a[$i]) = ", mycallback($a[$i]), "\n";
}

# using foreach
foreach my $x (@a) {
print "mycallback($x) = ", mycallback($x), "\n";
}

# using map (useful for transforming an array)
my @b = map mycallback($_), @a; # @b is now (2, 4, 6, 8, 10)

# and the same using an anonymous function
my @c = map { $_ * 2 } @a; # @c is now (2, 4, 6, 8, 10)

# use a callback stored in a variable
my $func = \&mycallback;
my @d = map &{$func}($_), @a; # @d is now (2, 4, 6, 8, 10)

==[[PHP]]==
[[Category:PHP]]

function cube($n)
{
return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);

== [[PL/SQL]] ==
[[Category:PL/SQL]]
'''Interpreter''' : Oracle compiler

set serveroutput on
declare
type myarray is table of number index by binary_integer;
x myarray;
i pls_integer;
begin
-- populate array
for i in 1..5 loop
x(i) := i;
end loop;
i :=0;
-- square array
loop
i := i + 1;
begin
x(i) := x(i)*x(i);
dbms_output.put_line(x(i));
exception
when no_data_found then exit;
end;
end loop;
end;
/

== [[Python]] ==
[[Category:Python]]
<pre>
def square(n):
return n * n
numbers = [1, 3, 5, 7]

squares1 = [square(n) for n in numbers] # list comprehension

squares2 = map(square, numbers) # discouraged nowadays

squares3 = [n * n for n in numbers] # no need for a function,
# anonymous or otherwise

isquares = (n * n for n in numbers) # iterator, lazy
</pre>

==[[Ruby]]==
[[Category:Ruby]]
# You could use a traditional "for i in arr" approach like below:
for i in [1,2,3,4,5] do
puts i**2
end

# Or you could the more preferred ruby way of an iterator (which is borrowed from SmallTalk)
[1,2,3,4,5].each{ |i| puts i**2 }

# To create a new array of each value squared
[1,2,3,4,5].map{ |i| i**2 }

==[[Scala]]==
[[Category:Scala]]
val l = List(1,2,3,4)
l.foreach {i => Console.println(i)}

Same for an array
val a = Array(1,2,3,4)
a.foreach {i => Console.println(i)}

// Or for an externally defined function
def doSomething(in: int) = {Console.println("Doing something with "+in)}
l.foreach(doSomething)

There is also a ''for'' syntax, which is internally rewritten to call foreach. A foreach method must be define on ''a''
for(val i <- a) Console.println(i)

It is also possible to apply a function on each item of an list to get a new list (same on array and most collections)
val squares = l.map{i => i * i} //returns List(1,4,9,16)

Or the equivalent ''for'' syntax, with the additional keyword ''yield'', map is called instead of foreach
val squares = for (val i <- l) yield i * i

== [[Scheme]] ==
[[Category:Scheme]]
(define (square n) (* n n))
(define x '(1 2 3 4 5))
(map square x)

''Please fix: '(1 2 3 4 5) is a list, not a vector''

A single-line variation
(map (lambda (n) (* n n)) '(1 2 3 4 5))

For completeness, the <tt>map</tt> function (which is R5RS standard) can be coded as follows:
(define (map f L)
(if (null? L)
L
(cons (f (car L)) (map f (cdr L)))))

== [[Smalltalk]] ==
[[Category:Smalltalk]]
| anArray |
anArray = #( 1 2 3 4 5 )
anArray do: [ :x | Transcript nextPut: x * x ]

== [[Tcl]] ==
[[Category:Tcl]]

If I wanted to call "<tt>myfunc</tt>" on each element of <tt>dat</tt> and <tt>dat</tt> were a list:

foreach var $dat { myfunc $var }

if <tt>dat</tt> were an array, however:

foreach var [array names dat] { myfunc $dat($var) }