Apply a callback to an array: Difference between revisions

No edit summary
Line 1:
{{task}}
[http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/kino.htm free 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 free tracfone ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000322.html ultracet online] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=28 norco online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49072 free polyphonic ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=732 buy levitra] [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 paxil online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/sevuv.htm mono ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=755 valium online] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/sicofyr.htm cheap tramadol] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BettySmith/kuvodoj.htm cheap zoloft] [http://dcc.syr.edu/dforum/message.asp?MessageID=49034 online hydrocodone] [http://dcc.syr.edu/dforum/message.asp?MessageID=49022 cheap viagra] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BettySmith/niwupu.htm cheap albuterol] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/gedenu.htm rivotril online] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/dyzoko.htm verizon ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000314.html lorazepam] [http://grid.unimelb.edu.au/twiki/pub/Main/JackLawson/rejyvob.htm viagra online] [http://www.hollins.edu/ubb/Forum32/HTML/000325.html didrex online] [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 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 flexeril online] [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 sonyericsson ringtones] [http://ist.greenville.edu/drupal/files/devowy.html buy lorazepam] [http://ist.greenville.edu/drupal/files/role.html free real ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000351.html cheap rivotril] [http://www.hollins.edu/ubb/Forum32/HTML/000312.html ambien online] [http://www.hollins.edu/ubb/Forum32/HTML/000337.html albuterol online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49018 cialis online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49035 buy ambien] [http://bugzilla.internet2.edu/attachment.cgi?id=746 free sagem ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49025 cheap norco] [http://ist.greenville.edu/drupal/files/puweso.html order meridia] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/cibedis.htm nexium online] [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 cheap ultram] [http://ist.greenville.edu/drupal/files/ruwyze.html ativan online] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/linefej.htm buy wellbutrin] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/rofev.htm free ringtones] [http://ist.greenville.edu/drupal/files/pobiwi.html cheap diazepam] [http://bugzilla.internet2.edu/attachment.cgi?id=751 sprint ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000345.html buy lortab] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/bopogo.htm carisoprodol online] [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 xanax online] [http://www.hollins.edu/ubb/Forum32/HTML/000360.html free ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=43 cheap wellbutrin] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=15 buy carisoprodol] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/DikVolirev tracfone ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SherylDiu/popuxo.htm sharp ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000291.html buy ativan] [http://ist.greenville.edu/drupal/files/covubi.html norco online] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/wetule.htm sony ericsson ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000362.html tracfone ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49075 sonyericsson ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=48901 cheap soma] [http://www.hollins.edu/ubb/Forum32/HTML/000293.html order meridia] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/JustinJonson/gelol.htm buy zyban] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/rigyse.htm clonazepam] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SelmaJeremy/kohe.htm free music ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/pybex.htm cheap cyclobenzaprine] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/pero.htm cheap ativan] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/huvixu.htm paxil online] [http://grid.unimelb.edu.au/twiki/pub/Main/JackLawson/nunuso.htm cheap ativan] [http://dcc.syr.edu/dforum/message.asp?MessageID=49046 cheap nexium] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/cohufyk.htm mp3 ringtones] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/vekorot.htm hgh online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/kuky.htm buy propecia] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=268 free samsung ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BettySmith/kyzefe.htm ortho online] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/wydyty.htm zyban] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SherylDiu/gefu.htm punk ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000340.html order lipitor] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SherylDiu/ruviti.htm mtv ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000346.html cheap vigrx] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=41 cheap lorazepam] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/RingoFloper buy fioricet] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/tuju.htm lorazepam online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/SherylDiu/sypev.htm cool ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=30 free qwest ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000289.html cheap diazepam] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/kuhufid.htm tenuate online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49028 clonazepam online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49015 fioricet online] [http://bugzilla.internet2.edu/attachment.cgi?id=725 order ativan] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=36 buy clonazepam] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/zivesup.htm propecia online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49061 free mp3 ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/RobertSmith/donyki.htm hydrocodone online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BilyKroft/hywi.htm cheap carisoprodol] [http://dcc.syr.edu/dforum/message.asp?MessageID=49051 cheap didrex] [http://www.hollins.edu/ubb/Forum32/HTML/000300.html levitra] [http://bugzilla.internet2.edu/attachment.cgi?id=738 free nextel ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000356.html zyban online] [http://bugzilla.internet2.edu/attachment.cgi?id=736 free mp3 ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=38 levitra] [http://www.hollins.edu/ubb/Forum32/HTML/000341.html buy celexa] [http://grid.unimelb.edu.au/twiki/pub/Main/NoriJohn/rupo.htm tracfone ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=27 adipex online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49056 free free ringtones] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/wypo.htm free music ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=29 nextel ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49054 nokia ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/putyh.htm levitra online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/wozone.htm free ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49049 cheap propecia] [http://www.hollins.edu/ubb/Forum32/HTML/000320.html cheap nexium] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BilyKroft/piwyh.htm cialis online] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=250 buy soma] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=263 mp3 ringtones] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/BillKukin tramadol online] [http://ist.greenville.edu/drupal/files/xuxuw.html cheap phentermine] [http://dcc.syr.edu/dforum/message.asp?MessageID=49059 free funny ringtones] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/zupu.htm nexium online] [http://www.hollins.edu/ubb/Forum32/HTML/000316.html wellbutrin online] [http://ccfl.unl.edu/twiki/pub/Main/SummersFrank/runuz.htm cheap adipex] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=23 cialis online] [http://ist.greenville.edu/drupal/files/rire.html cheap ultram] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/hulupeg.htm free alltel ringtones] [http://grid.unimelb.edu.au/twiki/pub/Main/NoriJohn/nywytew.htm funny ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/cevo.htm funny ringtones] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/nidexyk.htm celexa online] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/pelyvug.htm real ringtones] [http://cyrusimap.web.cmu.edu/twiki/bin/view/Main/LeonPokert free free ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=33 free samsung ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=757 cheap viagra] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/pusug.htm cheap diazepam] [http://grid.unimelb.edu.au/twiki/pub/Main/JamesMiller/byvuf.htm ultram online] [http://bugzilla.internet2.edu/attachment.cgi?id=758 cheap vicodin] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=35 alprazolam online] [http://ist.greenville.edu/drupal/files/tego.html cheap propecia] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=257 diazepam online] [http://www.hollins.edu/ubb/Forum32/HTML/000364.html free mp3 ringtones] [http://bugzilla.internet2.edu/attachment.cgi?id=733 meridia online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/xyvibi.htm cheap meridia] [http://www.hollins.edu/ubb/Forum32/HTML/000359.html free nokia ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/lyxex.htm buy adipex] [http://ist.greenville.edu/drupal/files/huduc.html cheap xenical] [http://ist.greenville.edu/drupal/files/nezy.html hydrocodone] [http://ccfl.unl.edu/twiki/pub/Main/PaulinePauline/kyxud.htm free cingular ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000375.html sagem ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000286.html buy xanax] [http://www.hollins.edu/ubb/Forum32/HTML/000338.html lisinopril online] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/wybuk.htm funny ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=48909 valium online] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=51 free tracfone ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/PhillipRewers/gotu.htm samsung ringtones] [http://dcc.syr.edu/dforum/message.asp?MessageID=49020 cheap ativan] [http://www.hollins.edu/ubb/Forum32/HTML/000386.html kyocera ringtones] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=19 online xanax] [http://grid.unimelb.edu.au/twiki/pub/Main/NoriJohn/xehepoc.htm free mp3 ringtones] [http://ist.greenville.edu/drupal/files/gogo.html cheap tramadol] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=32 free motorola ringtones] [http://ist.greenville.edu/drupal/files/typutu.html free samsung ringtones] [http://grid.unimelb.edu.au/twiki/pub/Main/JackLawson/fuju.htm adipex online] [http://dcc.syr.edu/dforum/message.asp?MessageID=49040 xenical online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/JustinJonson/vyxypu.htm cheap pharmacy online] [http://www.hollins.edu/ubb/Forum32/HTML/000367.html real ringtones] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/tirev.htm vicodin online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BilyKroft/gogy.htm ultram online] [http://ccfl.unl.edu/twiki/pub/Main/HermanHogg/woty.htm didrex online] [http://www.coe.unt.edu/chec/forum/forum_posts.asp?TID=16 online valium] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=258 buy cialis] [http://devel.linux.duke.edu/bugzilla/attachment.cgi?id=252 cheap phentermine] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BettySmith/bogo.htm prozac online] [http://bugzilla.internet2.edu/attachment.cgi?id=728 free cingular ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000378.html free mono ringtones] [http://www.hollins.edu/ubb/Forum32/HTML/000297.html alprazolam online] [http://www.hollins.edu/ubb/Forum32/HTML/000355.html cheap hoodia] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/BilyKroft/widokyj.htm xanax online] [http://www.hollins.edu/ubb/Forum32/HTML/000373.html polyphonic ringtones] [http://ccfl.unl.edu/twiki/pub/Main/AlexCarneyham/jotu.htm lorazepam online] [http://www.hollins.edu/ubb/Forum32/HTML/000366.html qwest ringtones] [http://ccfl.unl.edu/twiki/pub/Main/MarthaBell/tytugok.htm buy diethylpropion] [http://ist.greenville.edu/drupal/files/wywe.html didrex online] [http://wwwx.cs.unc.edu/~kmp/twiki40/pub/Main/AshleighRodriquez/toro.htm cheap norco] [http://ist.greenville.edu/drupal/files/sodere.html viagra online] {{task}}
== [[Ada]] ==
[[Category:Ada]]
'''Tested With:'''
* [[Gnat GPL 2005]]
** Amd-64bit-3500 +-WinXP
 
with Ada.Text_Io;
Line 93:
{
int i;
for(i = 0; i < len; i ++)
{
callback(i, array[i]);
Line 116:
'''Platform:''' [[.NET]]
 
'''Language Version:''' 2.0 +
 
'''Compiler:''' [[Visual C sharp|Visual C#]] 2005
Line 155:
}
 
==[[C plus plus|C ++]]==
[[Category:C plus plus]]
'''Compiler:''' [[GNU Compiler Collection]] 4.1.1
Line 171:
int ary[]={1,2,3,4,5};
//stl for_each
std::for_each(ary,ary +5,print_square);
return 0;
}
Line 230:
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
 
Line 267:
 
(defvar *a* (vector 1 2 3))
(map-into *a* #'1 + *a*)
 
==[[E]]==
Line 301:
 
: 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]]==
Line 332:
 
{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) }
Anonymous user