Apply a callback to an array: Difference between revisions
Content added Content deleted
(Undo revision 7898 by Special:Contributions/TucRup (User talk:TucRup)) |
mNo edit summary |
||
Line 1: | Line 1: | ||
[http://itsmeladuh.ifrance.com/content/view/cover-cd/ cover cd musicale] [http://mania555.ifrance.com/img/styles/il-resto.htm il resto del carlino marche] [http://soundwave106.ifrance.com/resources/articles/sentenza-di/ sentenza di morte] [http://csmith06.ifrance.com/lib/phone-manager.htm phone manager driver] [http://dgdeeper.ifrance.com/img/styles/zyxel-access.htm zyxel access] [http://dgdeeper.ifrance.com/img/styles/meliso.htm meliso] [http://alvonina.ifrance.com/img/styles/fotocamere-digitali/ fotocamere digitali yakumo] [http://blackblackblack.ifrance.com/lib/demolizioni-varese.htm demolizioni varese] [http://bladiblada.ifrance.com/resources/articles/oggetti-utili.htm oggetti utili] [http://hethrfethr77.ifrance.com/content/view/palla-bianca/ palla bianca] [http://faithib.ifrance.com/description/lib/scollo-a.htm scollo a v] [http://alvonina.ifrance.com/img/styles/cali-cumbia/ cali cumbia] [http://asing-argirl.ifrance.com/images/small/copertine-porno/ copertine porno] [http://kiravina.ifrance.com/html/dsp-yamaha.htm dsp yamaha] [http://dgdeeper.ifrance.com/img/styles/lg-rc.htm lg rc] [http://kiravina.ifrance.com/html/dido-wite.htm dido wite flag] [http://twobit77.ifrance.com/description/lib/prestito-imola/ prestito imola] [http://twobit77.ifrance.com/description/lib/www-la/ www la cultura dell illuminismo in europ] [http://mania555.ifrance.com/img/styles/storia-della.htm storia della parola informare] [http://blackblackblack.ifrance.com/lib/bed-and.htm bed and breakfast cagliari] [http://kirizz.ifrance.com/html/tatuaggio-cantante.htm tatuaggio cantante] [http://itsmeladuh.ifrance.com/content/view/chris/ chris] [http://crazybird027.ifrance.com/view/new/e-poi/ e poi mi parli di una vita insieme] [http://dgdeeper.ifrance.com/img/styles/testo-heaven.htm testo heaven brian adams] [http://hethrfethr77.ifrance.com/content/view/lettore-di/ lettore di card] [http://csmith06.ifrance.com/lib/palafitte.htm palafitte] [http://nettikyzarc.ifrance.com/content/view/aspirapolveri-con/ aspirapolveri con filtro hepa] [http://soundwave106.ifrance.com/resources/articles/cavo-da/ cavo da rgb a] [http://twobit77.ifrance.com/description/lib/puma-felpe/ puma felpe] [http://asing-argirl.ifrance.com/images/small/terratec-usb/ terratec usb digitale] [http://bladiblada.ifrance.com/resources/articles/viaggiavventurenelmondo.htm viaggiavventurenelmondo] [http://csmith06.ifrance.com/lib/simple-man.htm simple man] [http://blackblackblack.ifrance.com/lib/last-minute.htm last minute a ponza] [http://dgdeeper.ifrance.com/img/styles/olympus.htm olympus 80] [http://twobit77.ifrance.com/description/lib/lemon-tree/ lemon tree fools garden] [http://nettikyzarc.ifrance.com/content/view/camice/ camice] [http://faithib.ifrance.com/description/lib/racconti-collant.htm racconti collant] [http://faithib.ifrance.com/description/lib/numero-telefonico.htm numero telefonico] [http://kiravina.ifrance.com/html/mercedes-lombardia.htm mercedes lombardia] [http://mania555.ifrance.com/img/styles/l-isola.htm l isola che scotta] [http://asing-argirl.ifrance.com/images/small/vuelve-a/ vuelve a mi] [http://crazybird027.ifrance.com/view/new/un-angelo/ un angelo per satana] [http://nettikyzarc.ifrance.com/content/view/tarantapower/ tarantapower] [http://mania555.ifrance.com/img/styles/corsa-mega.htm corsa mega] [http://blackblackblack.ifrance.com/lib/vacanze-in.htm vacanze in barcellona] [http://kirizz.ifrance.com/html/barboncino-nano.htm barboncino nano] [http://dgdeeper.ifrance.com/img/styles/giochini-eseguibili.htm giochini eseguibili] [http://alvonina.ifrance.com/img/styles/fotocamere-analogiche/ fotocamere analogiche] [http://crazybird027.ifrance.com/view/new/video-di/ video di omicidi] [http://crazybird027.ifrance.com/view/new/don-omar/ don omar live v 1] [http://itsmeladuh.ifrance.com/content/view/diario-scuola/ diario scuola] [http://crazybird027.ifrance.com/view/new/il-pioniere/ il pioniere] [http://bladiblada.ifrance.com/resources/articles/cruscata.htm cruscata] [http://twobit77.ifrance.com/description/lib/societa-d/ societa d arme] [http://bladiblada.ifrance.com/resources/articles/televisori-con.htm televisori con dvd samsung] [http://bang-sigh.ifrance.com/images/small/replica-orologio/ replica orologio bulgari] [http://bladiblada.ifrance.com/resources/articles/lcd-.htm lcd 37 hdmi tv televisori] [http://reikofanel.ifrance.com/web/paris-hillton.htm paris hillton] [http://crazybird027.ifrance.com/view/new/dido-video/ dido video download] [http://nettikyzarc.ifrance.com/content/view/arredamento-/ arredamento forniture e accessori] [http://mania555.ifrance.com/img/styles/inno-brigata.htm inno brigata sassari dimonios] [http://faithib.ifrance.com/description/lib/haiduc-dragostea.htm haiduc dragostea di tei] [http://nettikyzarc.ifrance.com/content/view/multi-tap/ multi tap ps2] [http://blackblackblack.ifrance.com/lib/toshiba-satellite.htm toshiba satellite m40x 122] [http://dgdeeper.ifrance.com/img/styles/nokia-ac.htm nokia ac1] [http://hethrfethr77.ifrance.com/content/view/giochi-avventura/ giochi avventura gratis] [http://blackblackblack.ifrance.com/lib/colonne-sonore.htm colonne sonore delle pubblicita] [http://bang-sigh.ifrance.com/images/small/cartina-geografica/ cartina geografica di siena] [http://sarah531.ifrance.com/lib/unico-.htm unico 11 hp] [http://blackblackblack.ifrance.com/lib/jukebox-zen.htm jukebox zen] [http://kiravina.ifrance.com/html/yety-sport.htm yety sport] [http://twobit77.ifrance.com/description/lib/esame-avvocati/ esame avvocati genova 2003 risultati] [http://twobit77.ifrance.com/description/lib/il-tatuato/ il tatuato] [http://mania555.ifrance.com/img/styles/frigorifero-da.htm frigorifero da viaggio] [http://crazybird027.ifrance.com/view/new/prego/ prego] [http://blackblackblack.ifrance.com/lib/odocoileus.htm odocoileus] [http://soundwave106.ifrance.com/resources/articles/roberto-cavalli/ roberto cavalli calzature] [http://dgdeeper.ifrance.com/img/styles/prg-.htm prg 50 casio] [http://kiravina.ifrance.com/html/the-film.htm the film zone] [http://bladiblada.ifrance.com/resources/articles/preparativi-per.htm preparativi per le nozze] [http://dgdeeper.ifrance.com/img/styles/tv-lg.htm tv lg] [http://bang-sigh.ifrance.com/images/small/tatoo/ tatoo] [http://bladiblada.ifrance.com/resources/articles/torta-di.htm torta di riso e miele] [http://sarah531.ifrance.com/lib/gran-prix.htm gran prix] [http://twobit77.ifrance.com/description/lib/frigo-compatto/ frigo compatto] [http://dgdeeper.ifrance.com/img/styles/filtri-semimaschere.htm filtri semimaschere] [http://itsmeladuh.ifrance.com/content/view/monitor-lcd/ monitor lcd benq fp767] [http://reikofanel.ifrance.com/web/xg-flash.htm xg flash] [http://asing-argirl.ifrance.com/images/small/castelgrande/ castelgrande] [http://reikofanel.ifrance.com/web/ne-tice.htm ne tice me se mp3] [http://dgdeeper.ifrance.com/img/styles/non-aprite.htm non aprite quel cancello] [http://kirizz.ifrance.com/html/icet-o.htm icet o g] [http://hethrfethr77.ifrance.com/content/view/unibs/ unibs] [http://dgdeeper.ifrance.com/img/styles/cartuccia-canon.htm cartuccia canon s300] [http://nettikyzarc.ifrance.com/content/view/michelle-anderson/ michelle anderson] [http://kirizz.ifrance.com/html/pc-hp.htm pc hp media] [http://asing-argirl.ifrance.com/images/small/tubo-per/ tubo per piscina] [http://dgdeeper.ifrance.com/img/styles/ramatura.htm ramatura] [http://itsmeladuh.ifrance.com/content/view/bade/ bade] [http://bladiblada.ifrance.com/resources/articles/etichette-cd.htm etichette cd dvd] [http://twobit77.ifrance.com/description/lib/dance/ dance 1994] [http://alvonina.ifrance.com/img/styles/freemusic/ freemusic] [http://bladiblada.ifrance.com/resources/articles/video-on.htm video on demand] [http://alvonina.ifrance.com/img/styles/ufficio-tortona/ ufficio tortona] [http://reikofanel.ifrance.com/web/tetris-puzzle.htm tetris puzzle] [http://blackblackblack.ifrance.com/lib/hard-disk.htm hard disk maxtor 60] [http://bang-sigh.ifrance.com/images/small/brunello-banfi/ brunello banfi] [http://sarah531.ifrance.com/lib/corpse.htm corpse] [http://twobit77.ifrance.com/description/lib/franco-iv/ franco iv franco i] [http://sarah531.ifrance.com/lib/lorna-papi.htm lorna papi chulo] [http://soundwave106.ifrance.com/resources/articles/impianto-vivavoce/ impianto vivavoce] [http://alvonina.ifrance.com/img/styles/cartoon-bondage/ cartoon bondage] [http://twobit77.ifrance.com/description/lib/fergusson-robert/ fergusson robert] [http://blackblackblack.ifrance.com/lib/tottoko.htm tottoko] [http://faithib.ifrance.com/description/lib/melissa-vieni.htm melissa vieni a cantare] [http://kiravina.ifrance.com/html/sci-games.htm sci games in overdose] [http://soundwave106.ifrance.com/resources/articles/codice-gratis/ codice gratis sblocco a835] [http://itsmeladuh.ifrance.com/content/view/teatro-invito/ teatro invito] [http://asing-argirl.ifrance.com/images/small/calidad-de/ calidad de vida] [http://itsmeladuh.ifrance.com/content/view/tv-/ tv 29 100hz] [http://soundwave106.ifrance.com/resources/articles/turner-olympic/ turner olympic 1500 pro ei] [http://toupheefee.ifrance.com/text/directory/nord/ nord] [http://csmith06.ifrance.com/lib/files-absat.htm files absat seca] [http://twobit77.ifrance.com/description/lib/haier-jcgme/ haier jc165gme] [http://toupheefee.ifrance.com/text/directory/yitzhak-rabin/ yitzhak rabin] [http://soundwave106.ifrance.com/resources/articles/volume-muscolare/ volume muscolare integratori] [http://nettikyzarc.ifrance.com/content/view/cors/ cors] [http://nettikyzarc.ifrance.com/content/view/ji-ji/ ji ji ji redonditos] [http://crazybird027.ifrance.com/view/new/hotel-a/ hotel a tenerife las dalias] [http://crazybird027.ifrance.com/view/new/golf-iv/ golf iv gti 150 tdi] [http://asing-argirl.ifrance.com/images/small/volleyball-femminile/ volleyball femminile] [http://bladiblada.ifrance.com/resources/articles/televisori-lcd.htm televisori lcd 12v] [http://csmith06.ifrance.com/lib/italiane-rapite.htm italiane rapite in iraq] [http://toupheefee.ifrance.com/text/directory/tablas-las/ tablas las] [http://faithib.ifrance.com/description/lib/incasso-.htm incasso 12 coperti] [http://faithib.ifrance.com/description/lib/raf-ouch.htm raf ouch] [http://bladiblada.ifrance.com/resources/articles/nokia-s.htm nokia s 90] [http://nettikyzarc.ifrance.com/content/view/bepi-di/ bepi di marzi] [http://asing-argirl.ifrance.com/images/small/ferretti-andrea/ ferretti andrea ortopedico] [http://csmith06.ifrance.com/lib/dragonstea-din.htm dragonstea din stei] [http://toupheefee.ifrance.com/text/directory/foto-nick/ foto nick berg] [http://asing-argirl.ifrance.com/images/small/grande-fratello/ grande fratello 3] [http://blackblackblack.ifrance.com/lib/televisore-lowe.htm televisore lowe] [http://sarah531.ifrance.com/lib/rinascente.htm rinascente] [http://reikofanel.ifrance.com/web/maxtor-atlas.htm maxtor atlas 15k 36gb] [http://crazybird027.ifrance.com/view/new/matrimonio-auguri/ matrimonio auguri] [http://kirizz.ifrance.com/html/personal-jesus.htm personal jesus] [http://sarah531.ifrance.com/lib/gay-varese.htm gay varese] [http://twobit77.ifrance.com/description/lib/esterno-/ esterno 40 gb] [http://asing-argirl.ifrance.com/images/small/triestina-calcio/ triestina calcio] [http://blackblackblack.ifrance.com/lib/lettore-cd.htm lettore cd da tavolo compatibile mp3] [http://dgdeeper.ifrance.com/img/styles/tyan-thunder.htm tyan thunder i7505] [http://itsmeladuh.ifrance.com/content/view/splinder/ splinder] [http://dgdeeper.ifrance.com/img/styles/rapitori.htm rapitori] [http://nettikyzarc.ifrance.com/content/view/projection-screen/ projection screen] [http://nettikyzarc.ifrance.com/content/view/biglietto-aereo/ biglietto aereo roma bangkok] [http://hethrfethr77.ifrance.com/content/view/claudine/ claudine] [http://alvonina.ifrance.com/img/styles/norton-server/ norton server] [http://faithib.ifrance.com/description/lib/integratori-alimentari.htm integratori alimentari magnesio potassio] [http://kiravina.ifrance.com/html/mi-manchi.htm mi manchi accordi] [http://itsmeladuh.ifrance.com/content/view/norden/ norden] [http://mania555.ifrance.com/img/styles/business-card.htm business card scanner] [http://nettikyzarc.ifrance.com/content/view/scart-wireless/ scart wireless] [http://blackblackblack.ifrance.com/lib/elevation.htm elevation] [http://asing-argirl.ifrance.com/images/small/lamu-x/ lamu x] [http://reikofanel.ifrance.com/web/poliziotti-a.htm poliziotti a domicilio] [http://bang-sigh.ifrance.com/images/small/renou-louis/ renou louis] [http://hethrfethr77.ifrance.com/content/view/www-oneone/ www oneone] [http://alvonina.ifrance.com/img/styles/fornello-coleman/ fornello coleman] [http://nettikyzarc.ifrance.com/content/view/des-grieux/ des grieux] [http://csmith06.ifrance.com/lib/prezzo-auto.htm prezzo auto] [http://faithib.ifrance.com/description/lib/telefonini-sony.htm telefonini sony ericsson w800] [http://csmith06.ifrance.com/lib/win-tv.htm win tv usb schede acquisizione e tv] [http://csmith06.ifrance.com/lib/gli-ultimi.htm gli ultimi giorni] [http://faithib.ifrance.com/description/lib/e-c.htm e c m sanita] [http://nettikyzarc.ifrance.com/content/view/guanti/ guanti] [http://kiravina.ifrance.com/html/la-serva.htm la serva padrona] [http://itsmeladuh.ifrance.com/content/view/eas-myoplex/ eas myoplex carb sense] [http://soundwave106.ifrance.com/resources/articles/il-decalogo/ il decalogo sei] [http://blackblackblack.ifrance.com/lib/www-ashanti.htm www ashanti com] [http://faithib.ifrance.com/description/lib/eurorack-pro.htm eurorack pro rx1602] [http://dgdeeper.ifrance.com/img/styles/ieti.htm ieti] [http://bladiblada.ifrance.com/resources/articles/disegni-scouts.htm disegni scouts] [http://csmith06.ifrance.com/lib/parafrasi-umberto.htm parafrasi umberto saba] [http://kiravina.ifrance.com/html/www-videos.htm www videos mtv com] [http://alvonina.ifrance.com/img/styles/filmer-sir/ filmer sir robert] [http://twobit77.ifrance.com/description/lib/copertina-troy/ copertina troy] [http://faithib.ifrance.com/description/lib/il-re.htm il re leone 3] [http://sarah531.ifrance.com/lib/tania-rusoff.htm tania rusoff] [http://nettikyzarc.ifrance.com/content/view/gruppo-continuita/ gruppo continuita] [http://toupheefee.ifrance.com/text/directory/b-b/ b b nel salento] [http://sarah531.ifrance.com/lib/il-ballo.htm il ballo do patsy] [http://hethrfethr77.ifrance.com/content/view/olio-capelli/ olio capelli] [http://itsmeladuh.ifrance.com/content/view/al-arembaggio/ al arembaggio] [http://faithib.ifrance.com/description/lib/presuntos.htm presuntos] [http://csmith06.ifrance.com/lib/hesse.htm hesse] [http://mania555.ifrance.com/img/styles/fotos-pp.htm fotos pp] [http://twobit77.ifrance.com/description/lib/residenze-in/ residenze in costa smeralda] [http://hethrfethr77.ifrance.com/content/view/frases-celebres/ frases celebres] [http://reikofanel.ifrance.com/web/marechiaro.htm marechiaro] {{task}} |
|||
{{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 |