Apply a callback to an array: Difference between revisions

m
no edit summary
(revert spam)
mNo edit summary
Line 1:
[http://il-mistero.cleric4u.info il mistero della piramide] [http://remayfuy.airsofthost.com/images/small/sony-cpx/ sony cpx11] [http://amplificatori-akai.giantlau.info amplificatori akai] [http://gaychat.firethro.info gaychat] [http://alpaddy.airsofthost.com/img/styles/www-/ www 113 com] [http://maheinze.airsofthost.com/data/ww-.htm ww 130 it] [http://nana-flora.monbrenno.info nana flora] [http://lejenny.airsofthost.com/library/html/il-figlio.htm il figlio della giungla] [http://alpaddy.airsofthost.com/img/styles/ornavasso/ ornavasso] [http://remayfuy.airsofthost.com/images/small/frigo-teak/ frigo teak] [http://xaixai.monbrenno.info xaixai] [http://rumote.airsofthost.com/lib/squadra-azzurra.htm squadra azzurra] [http://fotoromanzi-porno.monbrenno.info fotoromanzi porno gratis] [http://remayfuy.airsofthost.com/images/small/video-musicale/ video musicale maroon 5] [http://lejenny.airsofthost.com/library/html/esterno-iomega.htm esterno iomega hard disk e floppy] [http://astilbe.giantlau.info astilbe] [http://remayfuy.airsofthost.com/images/small/ninna-oh/ ninna oh] [http://lejenny.airsofthost.com/library/html/mares-orologio.htm mares orologio] [http://frgrossm.airsofthost.com/topic/l-ape/ l ape maya in concerto] [http://laortiz.airsofthost.com/view/new/shopping-ascoli/ shopping ascoli piceno] [http://rumote.airsofthost.com/lib/culi-e.htm culi e cazzi super pelosi] [http://remayfuy.airsofthost.com/images/small/me-muero/ me muero] [http://hdurian.airsofthost.com/view/new/rode-k.htm rode k2] [http://usb-pen.otrev4ro.info usb pen drive 2 gb] [http://piatti-ceramica.otrev4ro.info piatti ceramica] [http://banzlees.airsofthost.com/description/lib/lg-.htm lg rz42px11 plasma] [http://rumote.airsofthost.com/lib/codec-per.htm codec per filmati dvx] [http://vanessa-cerezo.monbrenno.info vanessa cerezo] [http://lejenny.airsofthost.com/library/html/solaia.htm solaia 2002] [http://bejeweled-deluxe.monbrenno.info bejeweled deluxe] [http://golf-cabrio.giantlau.info golf cabrio] [http://frgrossm.airsofthost.com/topic/esecuzioni-di/ esecuzioni di americani] [http://hdurian.airsofthost.com/view/new/nuova-vitara.htm nuova vitara diesel auto nuove] [http://thes-lcd.cleric4u.info thes lcd] [http://corvo-rosso.cleric4u.info corvo rosso non avrai il mio scalpo] [http://frgrossm.airsofthost.com/topic/rex-frigoriferi/ rex frigoriferi da incasso] [http://remayfuy.airsofthost.com/images/small/scuola-danza/ scuola danza primavera] [http://tokina.otrev4ro.info tokina 12 24] [http://iuventus-in.monbrenno.info iuventus in latino] [http://alpaddy.airsofthost.com/img/styles/il-marito/ il marito] [http://remayfuy.airsofthost.com/images/small/i-video/ i video musicali di biagio antonacci] [http://viaggi-peru.firethro.info viaggi peru] [http://splucind.airsofthost.com/content/view/corte-d/ corte d appello di venezia] [http://ancora-una.cleric4u.info ancora una volta con sentimento] [http://giochi.cleric4u.info giochi 6630] [http://remayfuy.airsofthost.com/images/small/recueil-fossard/ recueil fossard] [http://maheinze.airsofthost.com/data/kris-bracciale.htm kris bracciale] [http://banzlees.airsofthost.com/description/lib/irak-decapitazioni.htm irak decapitazioni] [http://maheinze.airsofthost.com/data/cornice-quadro.htm cornice quadro] [http://rumote.airsofthost.com/lib/trattamento-dell.htm trattamento dell aria apparecchi e impianti] [http://frgrossm.airsofthost.com/topic/original-song/ original song] [http://frgrossm.airsofthost.com/topic/rivoluzione-demografica/ rivoluzione demografica] [http://video-porno.firethro.info video porno amatoriale gratis] [http://frgrossm.airsofthost.com/topic/tempo-di/ tempo di credere] [http://philips-az.cleric4u.info philips az 1310] [http://maheinze.airsofthost.com/data/bonney.htm bonney] [http://maheinze.airsofthost.com/data/digital-keys.htm digital keys] [http://lejenny.airsofthost.com/library/html/test-al.htm test al femminile] [http://hdurian.airsofthost.com/view/new/san-martino.htm san martino di ungaretti] [http://remix-obsesion.giantlau.info remix obsesion] [http://laortiz.airsofthost.com/view/new/bluetooth-palmare/ bluetooth palmare] [http://lejenny.airsofthost.com/library/html/cartolina-gratuite.htm cartolina gratuite] [http://banzlees.airsofthost.com/description/lib/il-carbone.htm il carbone come combustibile fossile] [http://rumote.airsofthost.com/lib/eros-amatoriale.htm eros amatoriale gratis] [http://www-linkami.otrev4ro.info www linkami net] [http://lejenny.airsofthost.com/library/html/que-ganaste.htm que ganaste] [http://banzlees.airsofthost.com/description/lib/fort-lauderdale.htm fort lauderdale] [http://www-napolimania.otrev4ro.info www napolimania it] [http://frgrossm.airsofthost.com/topic/www-pro/ www pro music it] [http://vasci-rossi.firethro.info vasci rossi] [http://here-she.cleric4u.info here she comes again] [http://splucind.airsofthost.com/content/view/kimco-people/ kimco people 250] [http://international-karate.monbrenno.info international karate plus] [http://god-of.firethro.info god of war ps2 videogiochi] [http://www-seven.cleric4u.info www seven com br] [http://remayfuy.airsofthost.com/images/small/mercedes-clk/ mercedes clk cdi] [http://traduzione-leave.otrev4ro.info traduzione leave jojo] [http://hdurian.airsofthost.com/view/new/bus.htm bus1] [http://siberia.monbrenno.info siberia] [http://hdurian.airsofthost.com/view/new/jazz-in.htm jazz in un giorno destate] [http://gruppo-dei.otrev4ro.info gruppo dei sei] [http://doctor-zhivago.monbrenno.info doctor zhivago] [http://banzlees.airsofthost.com/description/lib/demolizioni.htm demolizioni] [http://cueball.otrev4ro.info cueball] [http://scuola-inglese.giantlau.info scuola inglese on line] [http://lejenny.airsofthost.com/library/html/company-directors.htm company directors 2004] [http://convivendo-biagio.monbrenno.info convivendo biagio] [http://rumote.airsofthost.com/lib/albergo-istanbul.htm albergo istanbul] [http://argos-contro.cleric4u.info argos contro le 7 maschere di cera] [http://freecom-media.monbrenno.info freecom media player 3] [http://isegoria.cleric4u.info isegoria] [http://rumote.airsofthost.com/lib/bros.htm bros] [http://lejenny.airsofthost.com/library/html/travel-plug.htm travel plug adapter] [http://fick-august.otrev4ro.info fick august] [http://laortiz.airsofthost.com/view/new/correio-da/ correio da manha] [http://alpaddy.airsofthost.com/img/styles/latilla/ latilla] [http://lejenny.airsofthost.com/library/html/spartito-di.htm spartito di love story] [http://hdurian.airsofthost.com/view/new/rhodes-alexandre.htm rhodes alexandre de] [http://splucind.airsofthost.com/content/view/formato/ formato 2000] [http://frgrossm.airsofthost.com/topic/incontro-sexy/ incontro sexy] [http://uur-lives.firethro.info uur lives] [http://remayfuy.airsofthost.com/images/small/maria-la/ maria la o] [http://frgrossm.airsofthost.com/topic/sedrianese/ sedrianese] [http://rumote.airsofthost.com/lib/biglietti-aerei.htm biglietti aerei milano goa] [http://hdurian.airsofthost.com/view/new/ricevitori-audio.htm ricevitori audio video] [http://alpaddy.airsofthost.com/img/styles/figc-it/ figc it] [http://hdurian.airsofthost.com/view/new/norton-antivirus.htm norton antivirus gratis] [http://remayfuy.airsofthost.com/images/small/la-via/ la via del reiki] [http://disney-the.firethro.info disney the small one] [http://splucind.airsofthost.com/content/view/www-bluwin/ www bluwin ch] [http://remayfuy.airsofthost.com/images/small/kaza-lite/ kaza lite software] [http://hdurian.airsofthost.com/view/new/members-of.htm members of mayday] [http://banzlees.airsofthost.com/description/lib/san-antonio.htm san antonio hotel] [http://frgrossm.airsofthost.com/topic/venta-de/ venta de autos] [http://maheinze.airsofthost.com/data/tft-monitor.htm tft monitor 18] [http://te-i.giantlau.info te i pe] [http://videos-musicales.monbrenno.info videos musicales gratis] [http://laortiz.airsofthost.com/view/new/lo-straniero/ lo straniero misterioso] [http://rinaldi-cristina.monbrenno.info rinaldi cristina] [http://im-lost.monbrenno.info im lost without you] [http://seat-.firethro.info seat ibiza 14 16v 5p plus] [http://samsung-digimax.giantlau.info samsung digimax a400 digital camera] [http://frgrossm.airsofthost.com/topic/yakumo-dx/ yakumo dx4 divx] [http://belle-fighe.cleric4u.info belle fighe] [http://provincia-milano.otrev4ro.info provincia milano frassinetti] [http://laortiz.airsofthost.com/view/new/tatuaggi-fondoschiena/ tatuaggi fondoschiena] [http://grabster-av.otrev4ro.info grabster av250] [http://gioco-online.giantlau.info gioco online azione] [http://splucind.airsofthost.com/content/view/patrizia-rosetti/ patrizia rosetti video] [http://remayfuy.airsofthost.com/images/small/yamaha-del/ yamaha del peru] [http://maheinze.airsofthost.com/data/edizione-del.htm edizione del 07 11 2004 del centro] [http://pirelli-abbigliamento.otrev4ro.info pirelli abbigliamento] [http://hdurian.airsofthost.com/view/new/insegnanti-religione.htm insegnanti religione cattolica] [http://foto-di.cleric4u.info foto di tagli di capelli] [http://frgrossm.airsofthost.com/topic/valerio-merola/ valerio merola] [http://banzlees.airsofthost.com/description/lib/www-cobra.htm www cobra sor com] [http://hdurian.airsofthost.com/view/new/bagasciona.htm bagasciona] [http://unita-di.cleric4u.info unita di misura della temperatura] [http://laortiz.airsofthost.com/view/new/samos/ samos] [http://lejenny.airsofthost.com/library/html/vacanze-a.htm vacanze a fuerteventura] [http://rumote.airsofthost.com/lib/pda-phone.htm pda phone] [http://alpaddy.airsofthost.com/img/styles/psc-aurilium/ psc805 aurilium] [http://banzlees.airsofthost.com/description/lib/dfi-ultrad.htm dfi ultrad] [http://banzlees.airsofthost.com/description/lib/la-vedetta.htm la vedetta lombarda] [http://splucind.airsofthost.com/content/view/giochi-cellulare/ giochi cellulare gratis] [http://alpaddy.airsofthost.com/img/styles/giorgia-on/ giorgia on my mind] [http://alpaddy.airsofthost.com/img/styles/micro-hi/ micro hi cd dvd] [http://laortiz.airsofthost.com/view/new/insegnamenti-di/ insegnamenti di yoga tibetano] [http://hdurian.airsofthost.com/view/new/bracciale-uomo.htm bracciale uomo] [http://stampa-file.monbrenno.info stampa file binari] [http://splucind.airsofthost.com/content/view/tiziana-foschi/ tiziana foschi nuda] [http://remayfuy.airsofthost.com/images/small/vendita-sci/ vendita sci] [http://starsky-and.monbrenno.info starsky and hucth] [http://splucind.airsofthost.com/content/view/golf-/ golf 5 diesel] [http://rumote.airsofthost.com/lib/film-trans.htm film trans] [http://lejenny.airsofthost.com/library/html/navigatori-gps.htm navigatori gps garmin v] [http://banzlees.airsofthost.com/description/lib/renato-zero.htm renato zero cattura cd covers] [http://rumote.airsofthost.com/lib/sabrinaghio.htm sabrinaghio] [http://hdurian.airsofthost.com/view/new/alleghe.htm alleghe] [http://la-nave.giantlau.info la nave degli schiavi] [http://bigcock.cleric4u.info bigcock] [http://noi-fete.giantlau.info noi2 fete sexi] [http://phocus-wms.giantlau.info phocus wms] [http://avventura-e.monbrenno.info avventura e logica] [http://banzlees.airsofthost.com/description/lib/la-ex.htm la ex] [http://alpaddy.airsofthost.com/img/styles/lcd-videoproiettori/ lcd videoproiettori sanyo] [http://alpaddy.airsofthost.com/img/styles/fank/ fank] [http://frgrossm.airsofthost.com/topic/dance-mat/ dance mat] [http://hentai-gratis.giantlau.info hentai gratis] [http://alpaddy.airsofthost.com/img/styles/hotel-france/ hotel france] [http://maheinze.airsofthost.com/data/yu-gi.htm yu gi oh cards com] [http://satellite-m.monbrenno.info satellite m40146] [http://brainwaves.firethro.info brainwaves] [http://logitech-driving.otrev4ro.info logitech driving force] [http://frgrossm.airsofthost.com/topic/sissy/ sissy 6262] [http://laortiz.airsofthost.com/view/new/ipaq-wifi/ ipaq wifi] [http://imparare-italiano.monbrenno.info imparare italiano in italia] [http://laortiz.airsofthost.com/view/new/wc-elettrico/ wc elettrico] [http://motocross-mania.firethro.info motocross mania] [http://pcl-colori.giantlau.info pcl colori] [http://universita-di.giantlau.info universita di trieste farmacia] [http://splucind.airsofthost.com/content/view/manuale-macromedia/ manuale macromedia flash] [http://alpaddy.airsofthost.com/img/styles/pac-l/ pac l30] [http://frgrossm.airsofthost.com/topic/commenti-sul/ commenti sul libro di primo levi se questo e un uomo] [http://maheinze.airsofthost.com/data/corelli-audio.htm corelli audio analogue] [http://internet-con.otrev4ro.info internet con il cellulare] [http://lejenny.airsofthost.com/library/html/www-slovenia.htm www slovenia kompas] [http://jamellas-editor.giantlau.info jamellas editor] [http://gigaset-sl.otrev4ro.info gigaset sl 150 cordless] [http://laortiz.airsofthost.com/view/new/numeri-cellulari/ numeri cellulari] [http://hdurian.airsofthost.com/view/new/sandra-kristoff.htm sandra kristoff] [http://hdurian.airsofthost.com/view/new/testo-canzone.htm testo canzone fever buble] [http://sin-despertar.monbrenno.info sin despertar] [http://analisi-competitiva.giantlau.info analisi competitiva] [http://masterizzatori-dual.giantlau.info masterizzatori dual layer nec] [http://laortiz.airsofthost.com/view/new/djimon-hounsou/ djimon hounsou] [http://alpaddy.airsofthost.com/img/styles/un-nuevo/ un nuevo amor tranzas] [http://frgrossm.airsofthost.com/topic/w-w/ w w libero] [http://laortiz.airsofthost.com/view/new/sony-hdv/ sony hdv 1080i] [http://pantalone-pelle.monbrenno.info pantalone pelle] {{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 (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
Anonymous user