Apply a callback to an array: Difference between revisions

m
no edit summary
(revert spam)
mNo edit summary
Line 1:
[http://greensticky.freehostia.com/library/html/gattopardo/ gattopardo] [http://greensticky.freehostia.com/library/html/investimento-titolo/ investimento titolo stato] [http://www.freewebtown.com/boogiejames/topic/neon-farmacia.htm neon farmacia] [http://getraylo.freeweb7.com/img/styles/italia-toscana.htm italia toscana agriturismi] [http://gawelzba.t35.com/library/html/winx-libri.htm winx libri] [http://saline-joy.freehostia.com/web/iomega-desktop.htm iomega desktop hard drive 160 gb] [http://boreldar.t35.com/text/directory/le-spie.htm le spie dellaria] [http://mcwardlo.t35.com/library/html/deserto/ deserto] [http://gawelzba.t35.com/library/html/exz-casio.htm ex-z40 casio] [http://mcwardlo.t35.com/library/html/ortopedia-modena/ ortopedia modena] [http://mcwardlo.t35.com/library/html/rexton-/ rexton 2.7 xdi] [http://selemku.t35.com/text/directory/cheb-fares.htm cheb fares] [http://mauballe.125mb.com/web/la-collezione/ la collezione numismatica di banca] [http://getm0ney.freewebpage.org/lib/philips-toucam/ philips toucam fun ii - webcam usb 30] [http://selemku.t35.com/text/directory/ragazzo-gubbio.htm ragazzo gubbio] [http://www.freewebtown.com/boogiejames/topic/foto-di.htm foto di ragazze normali di napoli] [http://kopapguw.homeblock.com/topic/compact-flash/ compact flash 1 gb] [http://www.freewebtown.com/boogiejames/topic/dalla-parte.htm dalla parte di swann] [http://gawelzba.t35.com/library/html/eli-kokkinou.htm eli kokkinou] [http://greensticky.freehostia.com/library/html/la-filatrice/ la filatrice] [http://huruple.125mb.com/text/directory/da-incasso/ da incasso frigoriferi] [http://getm0ney.freewebpage.org/lib/sega-sesso/ sega sesso manuale] [http://flheddin.125mb.com/content/view/joan.htm joan] [http://boreldar.t35.com/text/directory/kyoga-lago.htm kyoga, lago-] [http://zoelverd.freeweb7.com/description/lib/copione-de/ copione de i cento passi] [http://mcwardlo.t35.com/library/html/polti/ polti 800] [http://lorcaxon.homeblock.com/data/yellow-le/ yellow, le cugine] [http://www.freewebtown.com/boogiejames/topic/elimina-annuncio.htm elimina annuncio] [http://mcwardlo.t35.com/library/html/ati-radeon/ ati radeon 9800 se 256mb] [http://www.freewebtown.com/boogiejames/topic/immagini-di.htm immagini di barche di varie marche] [http://grahrens.125mb.com/view/new/aperto-il/ aperto il sito di impossible creatures] [http://greensticky.freehostia.com/library/html/monica-adaro/ monica adaro] [http://wainfan.t35.com/view/new/video-scandalo.htm video scandalo cantante croata] [http://oedi.freehostia.com/text/directory/serena-del/ serena del grande fratello 4] [http://zoelverd.freeweb7.com/description/lib/ssteb/ ssteb] [http://selemku.t35.com/text/directory/hotel-ananas.htm hotel ananas vienna] [http://failtoexpress.freehostia.com/html/mitchell-silas.htm mitchell, silas weir] [http://getraylo.freeweb7.com/img/styles/ventole-x.htm ventole 8x8] [http://flheddin.125mb.com/content/view/x-mb.htm x700 256mb pci] [http://beshennayabelka.freehostia.com/content/view/annuncio-affitto.htm annuncio affitto belluno] [http://grahrens.125mb.com/view/new/foto-sexi/ foto sexi mara venier] [http://licohwar.homeblock.com/library/html/gli-alberi/ gli alberi non crescono fino in cielo] [http://mauballe.125mb.com/web/toner-selex/ toner selex 1120] [http://boreldar.t35.com/text/directory/san-fabio.htm san fabio martire] [http://failtoexpress.freehostia.com/html/nikon-afs.htm nikon af-s 24-120 vr ed] [http://www.freewebtown.com/prncessprncipe/library/html/ge.htm g.e.] [http://gawelzba.t35.com/library/html/arredo-giardino.htm arredo giardino mobili] [http://oedi.freehostia.com/text/directory/carneval-paris/ carneval paris] [http://www.freewebtown.com/starbriteyellow/resources/articles/crack-seriali.htm crack seriali] [http://licohwar.homeblock.com/library/html/rock-steady/ rock steady crew] [http://beshennayabelka.freehostia.com/content/view/un-americano.htm un americano tranquillo] [http://coarmago.t35.com/content/view/kock-paul/ kock, paul de-] [http://zoelverd.freeweb7.com/description/lib/notebook-toshiba/ notebook toshiba libretto u100] [http://www.freewebtown.com/boogiejames/topic/mamme-scopano.htm mamme scopano figli] [http://mauballe.125mb.com/web/mariana/ mariana] [http://scaleslea.freewebpage.org/text/directory/mondochat-it/ mondochat it] [http://kopapguw.homeblock.com/topic/asus-extreme/ asus extreme en6800] [http://mcwardlo.t35.com/library/html/daryl-hannah/ daryl hannah playboy] [http://huruple.125mb.com/text/directory/www-quest/ www quest net] [http://beshennayabelka.freehostia.com/content/view/la-scelta.htm la scelta di davy] [http://flheddin.125mb.com/content/view/raf-un.htm raf un grande salto] [http://gawelzba.t35.com/library/html/www-kamasuthra.htm www kamasuthra com] [http://www.freewebtown.com/prncessprncipe/library/html/snow-white.htm snow white] [http://grahrens.125mb.com/view/new/nike-silver/ nike silver] [http://grahrens.125mb.com/view/new/i-dont/ i dont wanna know house remix] [http://beshennayabelka.freehostia.com/content/view/continentale.htm continentale] [http://greensticky.freehostia.com/library/html/pb/ pb 6210] [http://boreldar.t35.com/text/directory/focus-km.htm focus km0] [http://www.freewebtown.com/starbriteyellow/resources/articles/hard-disk.htm hard disk 800 gb] [http://www.freewebtown.com/prncessprncipe/library/html/logitechmouse-mx.htm logitech-mouse mx 1000] [http://huruple.125mb.com/text/directory/piedimonte-san/ piedimonte san germano] [http://failtoexpress.freehostia.com/html/theather.htm theather] [http://huruple.125mb.com/text/directory/design-sito/ design sito web] [http://scaleslea.freewebpage.org/text/directory/lerrore-del/ lerrore del dio chang] [http://www.freewebtown.com/starbriteyellow/resources/articles/palle-gonfie.htm palle gonfie] [http://selemku.t35.com/text/directory/il-corsaro.htm il corsaro nero kids] [http://wainfan.t35.com/view/new/dvd-memory.htm dvd memory card] [http://lorcaxon.homeblock.com/data/affinita-nomi/ affinita nomi] [http://scaleslea.freewebpage.org/text/directory/pornomature/ pornomature] [http://www.freewebtown.com/boogiejames/topic/www-cai.htm www cai it] [http://mcwardlo.t35.com/library/html/cover-vasco/ cover vasco buoni e cattivi] [http://boreldar.t35.com/text/directory/scelte-pericolose.htm scelte pericolose] [http://zoelverd.freeweb7.com/description/lib/fifi-and/ fifi and romeo] [http://getraylo.freeweb7.com/img/styles/w-w.htm w w w petardas con] [http://www.freewebtown.com/boogiejames/topic/midi-supermix.htm midi supermix it] [http://www.freewebtown.com/boogiejames/topic/mi-stracci.htm mi stracci il cuore] [http://kopapguw.homeblock.com/topic/scarpe-donna/ scarpe donna con tacco alto dolce] [http://getraylo.freeweb7.com/img/styles/hp-allinone.htm hp all-in-one] [http://flheddin.125mb.com/content/view/cd-musicali.htm cd musicali da scaricare] [http://www.freewebtown.com/boogiejames/topic/ati-radeon.htm ati radeon x800 pro power color] [http://coarmago.t35.com/content/view/fatima/ fatima] [http://scaleslea.freewebpage.org/text/directory/lo-strano/ lo strano percorso degli 883] [http://flheddin.125mb.com/content/view/sigma-canon.htm sigma canon macro] [http://saline-joy.freehostia.com/web/modelli-auto.htm modelli auto] [http://greensticky.freehostia.com/library/html/plasma-ps/ plasma ps 42] [http://lorcaxon.homeblock.com/data/duke-nukem/ duke nukem advance] [http://www.freewebtown.com/prncessprncipe/library/html/philliphine.htm philliphine] [http://grahrens.125mb.com/view/new/latetia-casta/ latetia casta] [http://mauballe.125mb.com/web/batteria-gericom/ batteria gericom per4mance] [http://benevolently.freehostia.com/content/view/tivirus/ tivirus] [http://selemku.t35.com/text/directory/il-potere.htm il potere ferrarotti] [http://greensticky.freehostia.com/library/html/telemerida/ telemerida] [http://kopapguw.homeblock.com/topic/disneyworld/ disneyworld] [http://www.freewebtown.com/boogiejames/topic/game-boy.htm game boy advance cavo link] [http://getm0ney.freewebpage.org/lib/gina-g/ gina g] [http://boreldar.t35.com/text/directory/intel-pentium.htm intel pentium 4 3 2 478] [http://www.freewebtown.com/prncessprncipe/library/html/simpsons-road.htm simpsons road rage] [http://mauballe.125mb.com/web/an-ultra/ an8 ultra] [http://greensticky.freehostia.com/library/html/mp-usb/ mp3 usb sony] [http://www.freewebtown.com/prncessprncipe/library/html/omeostasi.htm omeostasi] [http://beshennayabelka.freehostia.com/content/view/bage-robert.htm bage, robert] [http://licohwar.homeblock.com/library/html/arredo-urbano/ arredo urbano] [http://getraylo.freeweb7.com/img/styles/ristoranti-parma.htm ristoranti parma] [http://www.freewebtown.com/prncessprncipe/library/html/annunci-feste.htm annunci feste coppie] [http://www.freewebtown.com/boogiejames/topic/hotel-conegliano.htm hotel conegliano] [http://coarmago.t35.com/content/view/hoteles-tlalpan/ hoteles tlalpan] [http://grahrens.125mb.com/view/new/clearvue-pdf/ clearvue pdf] [http://beshennayabelka.freehostia.com/content/view/palma-abbigliamento.htm palma abbigliamento verona] [http://www.freewebtown.com/starbriteyellow/resources/articles/juegos-gratis.htm juegos gratis de telefonica movil] [http://boreldar.t35.com/text/directory/testarda-io.htm testarda io] [http://boreldar.t35.com/text/directory/home-theatre.htm home theatre satellite] [http://zoelverd.freeweb7.com/description/lib/naksi/ naksi] [http://lorcaxon.homeblock.com/data/terminal/ terminal] [http://licohwar.homeblock.com/library/html/ohne-dish/ ohne dish] [http://boreldar.t35.com/text/directory/nude-in.htm nude in bagno] [http://huruple.125mb.com/text/directory/giochi-che/ giochi che si svolgono in casa] [http://coarmago.t35.com/content/view/acronis-true/ acronis true] [http://grahrens.125mb.com/view/new/ultravox/ ultravox] [http://licohwar.homeblock.com/library/html/hd-/ hd 250 gb esterno] [http://greensticky.freehostia.com/library/html/fak-it/ fak it] [http://scaleslea.freewebpage.org/text/directory/quando-eravamo/ quando eravamo re] [http://www.freewebtown.com/starbriteyellow/resources/articles/calma-e.htm calma e sangue freddo dirisio] [http://scaleslea.freewebpage.org/text/directory/truffaut-box/ truffaut box set] [http://scaleslea.freewebpage.org/text/directory/produttori-vino/ produttori vino] [http://boreldar.t35.com/text/directory/don-t.htm don t tell me traduzione] [http://beshennayabelka.freehostia.com/content/view/corsi-alta.htm corsi alta formazione] [http://lorcaxon.homeblock.com/data/dscp-sony/ dscp10 sony] [http://gawelzba.t35.com/library/html/peugeot.htm peugeot 206 1.4] [http://www.freewebtown.com/starbriteyellow/resources/articles/lampada-esterni.htm lampada esterni] [http://kopapguw.homeblock.com/topic/irradio/ irradio 2070] [http://lorcaxon.homeblock.com/data/pulsante/ pulsante] [http://grahrens.125mb.com/view/new/nuova-play/ nuova play station 2] [http://licohwar.homeblock.com/library/html/hera-comm/ hera comm] [http://failtoexpress.freehostia.com/html/www-limitedtoo.htm www limitedtoo com] [http://benevolently.freehostia.com/content/view/import-abbiglimento/ import abbiglimento] [http://oedi.freehostia.com/text/directory/e-toshiba/ e740 toshiba palmari] [http://lorcaxon.homeblock.com/data/adorable-lola/ adorable lola] [http://getm0ney.freewebpage.org/lib/lampada-videoproiettori/ lampada videoproiettori sanyo] [http://scaleslea.freewebpage.org/text/directory/athlon-/ athlon 64 socket939] [http://huruple.125mb.com/text/directory/telefe-it/ telefe it] [http://mauballe.125mb.com/web/ys-tech/ ys tech] [http://gawelzba.t35.com/library/html/campioni-reality.htm campioni reality show] [http://mauballe.125mb.com/web/tre-nelly/ tre nelly] [http://kopapguw.homeblock.com/topic/divx-recorder/ divx recorder hd] [http://getm0ney.freewebpage.org/lib/don-marino/ don marino barreto junior] [http://mcwardlo.t35.com/library/html/donne-pesanti/ donne pesanti] [http://getraylo.freeweb7.com/img/styles/lettore-di.htm lettore di compact flash] [http://www.freewebtown.com/boogiejames/topic/descrizione-savana.htm descrizione savana] [http://saline-joy.freehostia.com/web/sborrate-di.htm sborrate di cavalli] [http://beshennayabelka.freehostia.com/content/view/fogli-protocollo.htm fogli protocollo] [http://boreldar.t35.com/text/directory/claudia-kool.htm claudia kool] [http://grahrens.125mb.com/view/new/istituto-bancario/ istituto bancario san paolo] [http://boreldar.t35.com/text/directory/video-plajer.htm video plajer] [http://kopapguw.homeblock.com/topic/buena-vida/ buena vida delivery] [http://beshennayabelka.freehostia.com/content/view/grossi-oggetti.htm grossi oggetti] [http://lorcaxon.homeblock.com/data/suoinerie-nokia/ suoinerie nokia 3310] [http://zoelverd.freeweb7.com/description/lib/battletech/ battletech] [http://wainfan.t35.com/view/new/ready-or.htm ready or not] [http://scaleslea.freewebpage.org/text/directory/pc-manager/ pc manager] [http://licohwar.homeblock.com/library/html/hard-disk/ hard disk box] [http://failtoexpress.freehostia.com/html/tende-da.htm tende da camera] [http://mauballe.125mb.com/web/fuid-sistem/ fuid sistem] [http://licohwar.homeblock.com/library/html/toccata-e/ toccata e fuga in re minore bwv 565] [http://lorcaxon.homeblock.com/data/claudia-marsani/ claudia marsani] [http://lorcaxon.homeblock.com/data/club-porshe/ club porshe] [http://benevolently.freehostia.com/content/view/decapitazioni-ostaggi/ decapitazioni ostaggi in iraq] [http://failtoexpress.freehostia.com/html/diffusore-centrale.htm diffusore centrale tannoy] [http://lorcaxon.homeblock.com/data/guerriero-para/ guerriero para] [http://flheddin.125mb.com/content/view/follow-him.htm follow him] [http://grahrens.125mb.com/view/new/ford-focus/ ford focus usato] [http://gawelzba.t35.com/library/html/tetris-flash.htm tetris flash] [http://saline-joy.freehostia.com/web/vendita-ufficio.htm vendita ufficio lombardia] [http://scaleslea.freewebpage.org/text/directory/lovefool-dei/ lovefool dei cardigans] [http://saline-joy.freehostia.com/web/commerce-asp.htm commerce asp] [http://saline-joy.freehostia.com/web/karolina.htm karolina] [http://gawelzba.t35.com/library/html/la-coperta.htm la coperta azzurra] [http://huruple.125mb.com/text/directory/nv-gs/ nv gs250] [http://wainfan.t35.com/view/new/immagine-tatuaggio.htm immagine tatuaggio] [http://www.freewebtown.com/boogiejames/topic/scart-femmina.htm scart femmina] [http://zoelverd.freeweb7.com/description/lib/documentari/ documentari] [http://www.freewebtown.com/boogiejames/topic/dr-jazz.htm dr jazz] [http://zoelverd.freeweb7.com/description/lib/scarpette-puma/ scarpette puma] [http://coarmago.t35.com/content/view/village-peaple/ village peaple] [http://www.freewebtown.com/boogiejames/topic/isabel-madow.htm isabel madow] [http://www.freewebtown.com/prncessprncipe/library/html/piscine-comunali.htm piscine comunali] [http://flheddin.125mb.com/content/view/emmanuele.htm emmanuele] [http://zoelverd.freeweb7.com/description/lib/mario-monti/ mario monti] [http://getm0ney.freewebpage.org/lib/don-t/ don t you worry about a thing] [http://zoelverd.freeweb7.com/description/lib/collants/ collants] [http://www.freewebtown.com/prncessprncipe/library/html/senza-filo.htm senza filo cuffie] {{task}}
{{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)];
 
==[[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 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) }
 
== [[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
Anonymous user