Apply a callback to an array: Difference between revisions
Content added Content deleted
mNo edit summary |
(Undo revision 7509 by Special:Contributions/TucRup (User talk:TucRup)) |
||
Line 1: | Line 1: | ||
{{task}} |
|||
[http://cincydj.ifrance.com/images/small/donne-bielorusse/ donne bielorusse] [http://spirit-psyki.ifrance.com/lib/sexy-best.html sexy best] [http://doctorpinkerton.ifrance.com/lib/girls-sex/ girls sex anal vree] [http://tulip123456.ifrance.com/topic/naket.html naket] [http://mi-kkiechang.ifrance.com/una-ragazza.html una ragazza nuda - strip-tease] [http://end14april.ifrance.com/blog/blond-girls.html blond girls] [http://elixirium.ifrance.com/lib/donne-sopra.html donne sopra, femmine sotto] [http://adrienneee.ifrance.com/blog/foto-sexy/ foto sexy di ilaria d amico] [http://competitio.ifrance.com/porno-games/ porno games] [http://mi-kkiechang.ifrance.com/foto-canalis.html foto canalis convivendo] [http://elixirium.ifrance.com/lib/simonerossi.html simonerossi] [http://elixirium.ifrance.com/lib/piccole-donne.html piccole donne crescono] [http://cincydj.ifrance.com/images/small/creazione-sito/ creazione sito gratis] [http://doctorpinkerton.ifrance.com/lib/sesso-vaginale/ sesso vaginale] [http://bakadeshi.ifrance.com/img/styles/teensex.html teensex] [http://tulip123456.ifrance.com/topic/foto-.html foto gay] [http://tulip123456.ifrance.com/topic/fabio-uomini.html fabio uomini e donne] [http://spirit-psyki.ifrance.com/lib/foto-porno.html foto porno grtis] [http://spirit-psyki.ifrance.com/lib/sfondi-gratis.html sfondi gratis pc] [http://mi-kkiechang.ifrance.com/filmporno.html film,porno] [http://elixirium.ifrance.com/lib/cavalli-sesso.html cavalli sesso] [http://elixirium.ifrance.com/lib/sexo-esplicito.html sexo esplicito] [http://doctorpinkerton.ifrance.com/lib/hommes-desnudos/ hommes desnudos fotos] [http://tulip123456.ifrance.com/topic/racconti-incesto.html racconti incesto] [http://doctorpinkerton.ifrance.com/lib/elementi-di/ elementi di analisi matematica] [http://spirit-psyki.ifrance.com/lib/cy-girls.html cy girls] [http://bakadeshi.ifrance.com/img/styles/nice-girls.html nice girls] [http://spirit-psyki.ifrance.com/lib/george-arliss.html george arliss] [http://tulip123456.ifrance.com/topic/la-donna.html la donna matura piu sex] [http://cincydj.ifrance.com/images/small/settimanale-chi/ settimanale chi] [http://competitio.ifrance.com/video-porno/ video porno gay gratis] [http://end14april.ifrance.com/blog/floriana-nuda.html floriana nuda] [http://jnk713.ifrance.com/img/styles/ragazze-di/ ragazze di teramo] [http://bakadeshi.ifrance.com/img/styles/pregnant-sex.html pregnant sex] [http://furrysoap.ifrance.com/sex-video/ sex video free] [http://spirit-psyki.ifrance.com/lib/programmi-pc.html programmi pc gratis] [http://jnk713.ifrance.com/img/styles/porno-video/ porno video gratis] [http://jnk713.ifrance.com/img/styles/gioco-ps/ gioco ps2 gratis] [http://mi-kkiechang.ifrance.com/gothic-girl.html gothic girl] [http://jnk713.ifrance.com/img/styles/canal-codes/ canal codes] [http://cincydj.ifrance.com/images/small/japan-fuck/ japan fuck school girls] [http://adrienneee.ifrance.com/blog/fotogallery-grati/ fotogallery grati gay] [http://tulip123456.ifrance.com/topic/testo-canzone.html testo canzone fuck you right back] [http://jnk713.ifrance.com/img/styles/video-hey/ video hey sexy lady] [http://bakadeshi.ifrance.com/img/styles/videocamera-analogica.html videocamera analogica] [http://furrysoap.ifrance.com/diana-krall/ diana krall cover the girl in the other ro] [http://tulip123456.ifrance.com/topic/chiamami-per.html chiamami per sesso] [http://adrienneee.ifrance.com/blog/severina-porno/ severina porno video] [http://mi-kkiechang.ifrance.com/www-praia.html www praia do nudismo com br] [http://cincydj.ifrance.com/images/small/foto-annunci/ foto annunci erotici] [http://elixirium.ifrance.com/lib/porno-en.html porno en dibujos para enviar] [http://tulip123456.ifrance.com/topic/sexy-a.html sexy a tahiti] [http://bakadeshi.ifrance.com/img/styles/foto-di.html foto di piedi sexy] [http://bakadeshi.ifrance.com/img/styles/illegal-baby.html illegal baby sex] [http://adrienneee.ifrance.com/blog/chat-very/ chat very sexy] [http://jnk713.ifrance.com/img/styles/estudiantes-sexis/ estudiantes sexis] [http://competitio.ifrance.com/smallville-kristin/ smallville kristin kreuk nuda] [http://bakadeshi.ifrance.com/img/styles/free-sex.html free sex com] [http://mi-kkiechang.ifrance.com/bocche-sex.html bocche sex] [http://end14april.ifrance.com/blog/topless-sara.html topless sara ricci] [http://end14april.ifrance.com/blog/piss-gratis.html piss gratis] [http://adrienneee.ifrance.com/blog/photo-de/ photo de sexe] [http://cincydj.ifrance.com/images/small/onepiece-xxx/ onepiece xxx] [http://doctorpinkerton.ifrance.com/lib/programe-gratis/ programe gratis] [http://furrysoap.ifrance.com/www-oksesso/ www oksesso it] [http://mi-kkiechang.ifrance.com/video-erotici.html video erotici giorno] [http://furrysoap.ifrance.com/pporno-video/ pporno video] [http://adrienneee.ifrance.com/blog/gioco-gratis/ gioco gratis] [http://adrienneee.ifrance.com/blog/muzica-romaneasca/ muzica romaneasca download gratis] [http://jnk713.ifrance.com/img/styles/video-porno/ video porno di geena lee nolin] [http://elixirium.ifrance.com/lib/i-piloti.html i piloti del sesso] [http://bakadeshi.ifrance.com/img/styles/mas-porno.html mas porno] [http://competitio.ifrance.com/casalinghe-porno/ casalinghe porno] [http://elixirium.ifrance.com/lib/annuncio-personali.html annuncio personali ragazze] [http://adrienneee.ifrance.com/blog/sex/ sex 2006] [http://end14april.ifrance.com/blog/sex-now.html sex now] [http://tulip123456.ifrance.com/topic/www-tuttosessogratis.html www tuttosessogratis com] [http://doctorpinkerton.ifrance.com/lib/midi-spot/ midi spot vodafone gratis] [http://jnk713.ifrance.com/img/styles/foto-di/ foto di gay] [http://tulip123456.ifrance.com/topic/sex-videoclip.html sex videoclip] [http://jnk713.ifrance.com/img/styles/giocatori-nudi/ giocatori nudi foto] [http://spirit-psyki.ifrance.com/lib/cindy-crawford.html cindy crawford nuda] [http://competitio.ifrance.com/incontri-sesso/ incontri sesso foggia] [http://tulip123456.ifrance.com/topic/annunci-ragazze.html annunci ragazze cinesi] [http://jnk713.ifrance.com/img/styles/troie-donne/ troie donne 40] [http://furrysoap.ifrance.com/sesso-zoo/ sesso zoo] [http://adrienneee.ifrance.com/blog/scaricare-dvx/ scaricare dvx gratis] [http://tulip123456.ifrance.com/topic/le-ragazze.html le ragazze] [http://end14april.ifrance.com/blog/free-porno.html free porno movie] [http://jnk713.ifrance.com/img/styles/italia-porno/ italia porno] [http://bakadeshi.ifrance.com/img/styles/migliori-siti.html migliori siti trans gratis] [http://spirit-psyki.ifrance.com/lib/giornaliste-sexy.html giornaliste sexy] [http://end14april.ifrance.com/blog/uomo-nudi.html uomo nudi] [http://doctorpinkerton.ifrance.com/lib/suonerie-polifoniche/ suonerie polifoniche da scaricare gratis] [http://doctorpinkerton.ifrance.com/lib/video-anal/ video anal] [http://adrienneee.ifrance.com/blog/gemelle-olsen/ gemelle olsen sexy] [http://adrienneee.ifrance.com/blog/www-canal/ www canal disney com] [http://cincydj.ifrance.com/images/small/coca-cola/ coca cola girl] [http://end14april.ifrance.com/blog/porno-web.html porno web cam] [http://mi-kkiechang.ifrance.com/jet-are.html jet are you gonna be my girl] [http://mi-kkiechang.ifrance.com/pornografia-pura.html pornografia pura] [http://doctorpinkerton.ifrance.com/lib/girls-sex/ girls sex] [http://competitio.ifrance.com/foto-ragazzi/ foto ragazzi gratis] [http://doctorpinkerton.ifrance.com/lib/pinkword-sex/ pinkword sex] [http://cincydj.ifrance.com/images/small/gloria-gaynor/ gloria gaynor the ultimate collection] [http://furrysoap.ifrance.com/ripley-george/ ripley, george] [http://end14april.ifrance.com/blog/eros-ramazzottti.html eros ramazzottti] [http://mi-kkiechang.ifrance.com/download-california.html download california mp3 gratis] [http://tulip123456.ifrance.com/topic/strippoker-gratis.html strippoker gratis] [http://mi-kkiechang.ifrance.com/janet-jackson.html janet jackson nuda] [http://adrienneee.ifrance.com/blog/chat-gay/ chat gay] [http://tulip123456.ifrance.com/topic/macdonald-george.html macdonald, george] [http://bakadeshi.ifrance.com/img/styles/foto-porno.html foto porno adolescenti gay] [http://adrienneee.ifrance.com/blog/suoneria-nokia/ suoneria nokia gratis] [http://bakadeshi.ifrance.com/img/styles/video-porno.html video porno di celebrita] [http://adrienneee.ifrance.com/blog/show-girl/ show girl t v] [http://doctorpinkerton.ifrance.com/lib/donne-incatenate/ donne incatenate] [http://cincydj.ifrance.com/images/small/foto-playboy/ foto playboy nudi] [http://mi-kkiechang.ifrance.com/scaricare-gioco.html scaricare gioco gratis] [http://mi-kkiechang.ifrance.com/spiaggia-nudista.html spiaggia nudista] [http://doctorpinkerton.ifrance.com/lib/piedi-e/ piedi e sesso] [http://tulip123456.ifrance.com/topic/ricevitore-analogico.html ricevitore analogico] [http://elixirium.ifrance.com/lib/ragazze-nude.html ragazze nude completamente di 13 anni] [http://jnk713.ifrance.com/img/styles/key-canal/ key canal satellite] [http://tulip123456.ifrance.com/topic/avventure-sexy.html avventure sexy] [http://spirit-psyki.ifrance.com/lib/emilia-bisex.html emilia bisex] [http://jnk713.ifrance.com/img/styles/foto-porno/ foto porno di emma watson] [http://cincydj.ifrance.com/images/small/foto-trans/ foto trans bisex] [http://elixirium.ifrance.com/lib/linsday-lohan.html linsday lohan nuda] [http://bakadeshi.ifrance.com/img/styles/sexy-foto.html sexy foto ass] [http://end14april.ifrance.com/blog/boubble-girl.html boubble girl] [http://adrienneee.ifrance.com/blog/free-porno/ free porno ideo] [http://tulip123456.ifrance.com/topic/sexual-revolution.html sexual revolution] [http://adrienneee.ifrance.com/blog/femme-pute/ femme pute xxx] [http://mi-kkiechang.ifrance.com/kazaa-lite.html kazaa lite gratis scarica] [http://furrysoap.ifrance.com/foto-porno/ foto porno brunetta] [http://bakadeshi.ifrance.com/img/styles/gioco-erotico.html gioco erotico gratis] [http://tulip123456.ifrance.com/topic/ragazze-hostes.html ragazze hostes] [http://end14april.ifrance.com/blog/annuncio-erotici.html annuncio erotici gratis] [http://tulip123456.ifrance.com/topic/ragazze-sotto.html ragazze sotto chiave] [http://doctorpinkerton.ifrance.com/lib/foto-donne/ foto donne mature over40] [http://tulip123456.ifrance.com/topic/bosna-sex.html bosna sex] [http://tulip123456.ifrance.com/topic/gyno-lesbo.html gyno lesbo] [http://jnk713.ifrance.com/img/styles/sexyasia/ sexyasia] [http://doctorpinkerton.ifrance.com/lib/gay-skinheads/ gay skinheads sex free gallerie] [http://spirit-psyki.ifrance.com/lib/sardegna-vacanza.html sardegna vacanza bambino gratis] [http://bakadeshi.ifrance.com/img/styles/download-immaggini.html download immaggini porno amatoriali] [http://end14april.ifrance.com/blog/giochi-per.html giochi per adulti sul sesso] [http://tulip123456.ifrance.com/topic/canalecinque-it.html canalecinque it] [http://jnk713.ifrance.com/img/styles/ugo-foscolo/ ugo foscolo analisi del testo in morte d] [http://furrysoap.ifrance.com/donne-cerca/ donne cerca uomo] [http://mi-kkiechang.ifrance.com/purchasing-analyst.html purchasing analyst (regione piemonte - torino provincia)] [http://end14april.ifrance.com/blog/sesso-telefonico.html sesso telefonico con un uomo] [http://tulip123456.ifrance.com/topic/children-sexual.html children sexual photos] [http://jnk713.ifrance.com/img/styles/vignette-e/ vignette e cartoni animati sexy] [http://spirit-psyki.ifrance.com/lib/ver-video.html ver video porno de michelle vieth] [http://competitio.ifrance.com/tutto-su/ tutto su eros ramazotti] [http://furrysoap.ifrance.com/porno-mpeg/ porno mpeg] [http://doctorpinkerton.ifrance.com/lib/basi-midi/ basi midi file gratis] [http://jnk713.ifrance.com/img/styles/porno-porcate/ porno porcate] [http://mi-kkiechang.ifrance.com/hombres-gay.html hombres gay] [http://mi-kkiechang.ifrance.com/ph-sex.html ph sex] [http://elixirium.ifrance.com/lib/video-porno.html video porno cristina bella] [http://competitio.ifrance.com/bubble-girlz/ bubble girlz] [http://furrysoap.ifrance.com/download-winzip/ download winzip gratis] [http://jnk713.ifrance.com/img/styles/ribemontdessaignes-georges/ ribemont-dessaignes, georges] [http://jnk713.ifrance.com/img/styles/sex-revolucion/ sex revolucion] [http://spirit-psyki.ifrance.com/lib/tuttogratis.html tuttogratis] [http://doctorpinkerton.ifrance.com/lib/anal-cum/ anal cum] [http://furrysoap.ifrance.com/donne-legnano/ donne legnano] [http://competitio.ifrance.com/donne-mature/ donne mature con foto] [http://bakadeshi.ifrance.com/img/styles/sesso-cam.html sesso cam] [http://bakadeshi.ifrance.com/img/styles/foto-super.html foto super tette] [http://tulip123456.ifrance.com/topic/calendari-donne.html calendari donne nude 2005] [http://competitio.ifrance.com/donne-mature/ donne mature it] [http://tulip123456.ifrance.com/topic/www-gay.html www gay pride roma] [http://doctorpinkerton.ifrance.com/lib/www-pornografia/ www pornografia org com] [http://adrienneee.ifrance.com/blog/serena-gf/ serena gf nuda] [http://adrienneee.ifrance.com/blog/www-donne/ www donne nere bz] [http://adrienneee.ifrance.com/blog/annuncio-gratis/ annuncio gratis casa vacanza affitto] [http://jnk713.ifrance.com/img/styles/girls-gallery/ girls gallery] [http://mi-kkiechang.ifrance.com/mujeres-desnudas.html mujeres desnudas] [http://competitio.ifrance.com/le-donne/ le donne sul tetto] [http://jnk713.ifrance.com/img/styles/uomini-sportivi/ uomini sportivi foto xxx] [http://furrysoap.ifrance.com/picsex/ picsex] [http://tulip123456.ifrance.com/topic/first-time.html first time sex] [http://adrienneee.ifrance.com/blog/file-mp/ file mp3 gratis] [http://spirit-psyki.ifrance.com/lib/sexiran.html sexiran] [http://doctorpinkerton.ifrance.com/lib/sesso-sporco/ sesso sporco] [http://jnk713.ifrance.com/img/styles/michell-vieth/ michell vieth videos gratis] [http://elixirium.ifrance.com/lib/il-belo.html il belo delle donne] [http://tulip123456.ifrance.com/topic/data-base.html data base gratis] [http://mi-kkiechang.ifrance.com/eros-ang.html eros ang glamour] [http://end14april.ifrance.com/blog/girl-addio.html girl addio al celibato] [http://doctorpinkerton.ifrance.com/lib/freee-porno/ freee porno] [http://spirit-psyki.ifrance.com/lib/futurama-foto.html futurama foto porno] [http://bakadeshi.ifrance.com/img/styles/ragazze-a.html ragazze a mano armata] [http://tulip123456.ifrance.com/topic/donne-belle.html donne belle a sassari] [http://bakadeshi.ifrance.com/img/styles/calendari-porno.html calendari porno gratis] [http://tulip123456.ifrance.com/topic/sesso-ingoio.html sesso ingoio gratis] {{task}} |
|||
== [[Ada]] == |
== [[Ada]] == |
||
[[Category:Ada]] |
[[Category:Ada]] |
||
'''Tested With:''' |
'''Tested With:''' |
||
* [[Gnat GPL 2005]] |
* [[Gnat GPL 2005]] |
||
** Amd-64bit-3500 |
** 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 |
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 = |
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 |
(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 |
-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 |
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)]; |
|||
==[[Lua]]== |
|||
[[Category:Lua]] |
|||
Say we have an array: |
|||
myArray = {1, 2, 3, 4, 5} |
|||
A map function for this would be |
|||
map = function(f, data) |
|||
local result = {} |
|||
for k,v in ipairs(data) do |
|||
result[k] = f(v) |
|||
end |
|||
return result |
|||
end |
|||
Together with our array and and a square function this yields: |
|||
myFunc = function(x) return x*x end |
|||
print(unpack( map(myFunc, myArray) )) |
|||
--> 1 4 9 16 25 |
|||
If you used pairs() instead of ipairs(), this would even work on a hash table in general. |
|||
== [[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; |
|||
/ |
|||
==[[Pop11]]== |
|||
[[Category:Pop11]] |
|||
;;; Define a procedure |
|||
define proc(x); |
|||
printf(x*x, '%p,'); |
|||
enddefine; |
|||
;;; Create array |
|||
lvars ar = { 1 2 3 4 5}; |
|||
;;; Apply procedure to array |
|||
appdata(ar, proc); |
|||
If one wants to create a new array consisting of transformed values |
|||
then procedure mapdata may be more convenient. |
|||
== [[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 (vector->list x)) |
|||
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) } |
|||
== [[Toka]] == |
|||
[[Category:Toka]] |
|||
( array count function -- ) |
|||
{ |
|||
variable| array fn | |
|||
[ i 1- array @ ] is I |
|||
[ fn ! swap array ! [ I get-element fn @ invoke I put-element ] +iterate ] |
|||
} is map-array |
|||
( Build an array ) |
|||
5 cells is-array a |
|||
10 0 a put-element |
|||
11 1 a put-element |
|||
12 2 a put-element |
|||
13 3 a put-element |
|||
14 4 a put-element |
|||
( Add 1 to each item in the array ) |
|||
a 5 ` 1+ map-array |