Jump to content

Apply a callback to an array: Difference between revisions

revert spam
mNo edit summary
(revert spam)
Line 1:
{{task}}
[http://snailtail.cn/x-iso/index.html x iso] [http://kitmun.cn/young-neil.html young neil] [http://snailtail.cn/www-ruffo-trasporti/index.html www ruffo trasporti it] [http://kitmun.cn/zarawhites.html zarawhites] [http://kitmun.cn/yusuf.html yusuf] [http://wonted.cn/www-nick/index.html www nick berg] [http://wonted.cn/www-lorena/index.html www lorena mckennitt] [http://wonted.cn/www-xploder/index.html www xploder net] [http://quoits.cn/accadde-una-sera.html accadde una sera] [http://knock-knock.cn/www-ricerche/index.html www ricerche sull amicizia it] [http://beyoncenaked.kittiss.cn/index.html beyonce naked] [http://banescobancouniversal.midways.cn/index.html banesco banco universal] [http://bachatasalsamerengue.midways.cn/index.html bachata salsa merengue] [http://beathels.romanikki.cn/index.html beathels] [http://quoits.cn/atto-di-pirateria.html atto di pirateria] [http://kitmun.cn/yessica.html yessica toscanini] [http://snailtail.cn/xanga-skins/index.html xanga skins] [http://bdsmtortureaschiave.kittiss.cn/index.html bdsm torture a schiave] [http://blumyschadoow.clung.cn/index.html blu my schadoow] [http://wonted.cn/ww-tonale-it/index.html ww tonale it] [http://beverly500eaccessori.romanikki.cn/index.html beverly 500 e accessori] [http://kitmun.cn/zharick.html zharick] [http://blazeofglorymp3.clung.cn/index.html blaze of glory mp3] [http://wonted.cn/www-goegel/index.html www goegel] [http://blues.romanikki.cn/index.html blues] [http://knock-knock.cn/www-nickiloden/index.html www nickiloden com] [http://barrabarra.romanikki.cn/index.html barra barra] [http://baralehard.kittiss.cn/index.html barale hard] [http://knock-knock.cn/www-samsun/index.html www samsun com] [http://britnayspearsvideomusicali.kittiss.cn/index.html britnay spears video musicali] [http://bedendbreakfastmonzaedintorni.clung.cn/index.html bed end breakfast monza e dintorni] [http://kitmun.cn/yu-gi-oh.html yu gi oh versione italiana] [http://quoits.cn/a-chi-tocca.html a chi tocca tocca...!] [http://boxerdogs.fast-road.cn/index.html boxer dogs] [http://knock-knock.cn/www-santamariaischia/index.html www santamariaischia it] [http://brixiatour.fast-road.cn/index.html brixiatour] [http://wonted.cn/www-fapa-com/index.html www fapa com] [http://wonted.cn/wawes-of/index.html wawes of lov] [http://quoits.cn/zitti-zitti.html zitti zitti] [http://wonted.cn/www-chinesesex/index.html www chinesesex com] [http://wonted.cn/www-seefield/index.html www seefield com] [http://snailtail.cn/yab/index.html yab] [http://beyondthesea.fast-road.cn/index.html beyond the sea] [http://berghbeheading.kittiss.cn/index.html bergh beheading] [http://knock-knock.cn/www-cdc-org/index.html www cdc org] [http://wonted.cn/www-stib-be/index.html www stib be] [http://snailtail.cn/yu-gi-ho-carte/index.html yu gi ho carte] [http://snailtail.cn/xvid-ps2/index.html xvid ps2] [http://kitmun.cn/yespica-hard.html yespica hard] [http://quoits.cn/amarsi-un.html amarsi un po'...] [http://snailtail.cn/xxxena/index.html xxxena] [http://barbarastr.romanikki.cn/index.html barbara str] [http://bennybenassicaliforniadreaming.romanikki.cn/index.html benny benassi california dreaming] [http://blink1823.fast-road.cn/index.html blink 1823] [http://wonted.cn/www-titanium/index.html www titanium card] [http://quoits.cn/zorras.html zorras] [http://barbarachiappinisexy.clung.cn/index.html barbara chiappini sexy] [http://babbusoberanu.midways.cn/index.html babbu soberanu] [http://blueeyeslimpbiskit.midways.cn/index.html blue eyes limp biskit] [http://snailtail.cn/www-romagnanotte/index.html www romagnanotte com] [http://knock-knock.cn/www-transessuali/index.html www transessuali it] [http://bigliettiauguri.midways.cn/index.html biglietti auguri] [http://bluefeatladebubblinmp3.clung.cn/index.html blue feat l a d e bubblin mp3] [http://babbybash.fast-road.cn/index.html babby bash] [http://quoits.cn/all'ombra-del.html all'ombra del ricatto] [http://snailtail.cn/www-publitalia/index.html www publitalia it] [http://bluestandup.midways.cn/index.html blue stand up] [http://kitmun.cn/zum-zum.html zum zum zum mina] [http://knock-knock.cn/www-colombia/index.html www colombia com] [http://braccodegraci.fast-road.cn/index.html bracco de graci] [http://bradpaisley.clung.cn/index.html brad paisley] [http://snailtail.cn/x-cam/index.html x cam] [http://barzell.kittiss.cn/index.html barzell] [http://bosoni.kittiss.cn/index.html bosoni] [http://bluster.clung.cn/index.html bluster] [http://knock-knock.cn/www-ujga-com/index.html www ujga com] [http://knock-knock.cn/www-negozi/index.html www negozi afgani it] [http://wonted.cn/www-cinghiali/index.html www cinghiali it] [http://snailtail.cn/xcam-codes/index.html xcam codes] [http://knock-knock.cn/www-coku/index.html www coku it] [http://belleragazze.kittiss.cn/index.html belle ragazze] [http://biancanevesettenani.romanikki.cn/index.html biancaneve sette nani] [http://bl.romanikki.cn/index.html bl] [http://kitmun.cn/you-put.html you put a move on my heart] [http://snailtail.cn/xy-mara-venier/index.html xy mara venier] [http://buonaseradottore.clung.cn/index.html buona sera dottore] [http://kitmun.cn/you-touch-me.html you touch me tralala] [http://bassefrequenze.midways.cn/index.html basse frequenze] [http://balladeforadelline.clung.cn/index.html ballade for adelline] [http://snailtail.cn/y-sin-embargo/index.html y sin embargo] [http://wonted.cn/www-rulk/index.html www rulk com] [http://snailtail.cn/xp-home-edition/index.html xp home edition] [http://knock-knock.cn/www-maghiestreghe/index.html www maghiestreghe it] [http://bungejump.clung.cn/index.html bungejump] [http://kitmun.cn/yui-toshiki.html yui toshiki] [http://back.romanikki.cn/index.html back] [http://breathemidgeure.clung.cn/index.html breathe midge ure] [http://baccodicaravaggio.romanikki.cn/index.html bacco di caravaggio] [http://quoits.cn/amare-è....html amare è...] [http://bidibidibombommidi.midways.cn/index.html bidi bidi bom bom midi] [http://beverlymithell.fast-road.cn/index.html beverly mithell] [http://knock-knock.cn/www-taormina/index.html www taormina it] [http://snailtail.cn/xxx-man-sex/index.html xxx man sex vree] [http://knock-knock.cn/www-belliebrutti/index.html www belliebrutti it] [http://britneyeincinta.romanikki.cn/index.html britney e incinta] [http://quoits.cn/arriva-fra'.html arriva fra' cristoforo] [http://biondomarjorie.fast-road.cn/index.html biondo marjorie] [http://bibliotecadeisanti.kittiss.cn/index.html biblioteca dei santi] [http://wonted.cn/www-tele2-it/index.html www tele2 it] [http://snailtail.cn/www-girodivite/index.html www girodivite it] [http://quoits.cn/zacarias.html zacarias ferreira mid] [http://quoits.cn/zeljko-jokisimovic.html zeljko jokisimovic] [http://bnsfcom.clung.cn/index.html bnsf com] [http://knock-knock.cn/www-liceopontecorvo/index.html www liceopontecorvo it] [http://beninrepubblica.midways.cn/index.html beninrepubblica] [http://wonted.cn/www-cartoon/index.html www cartoon net workgames com] [http://buscarsobreliturgia.fast-road.cn/index.html buscar sobre liturgia] [http://bwv999.midways.cn/index.html bwv 999] [http://bastardsword.clung.cn/index.html bastard sword] [http://snailtail.cn/www-ilnuovo/index.html www ilnuovo redaweb it] [http://bobmarley.kittiss.cn/index.html bobmarley] [http://snailtail.cn/yeti-sport-trucchi/index.html yeti sport trucchi] [http://wonted.cn/www-sbn-it/index.html www sbn it] [http://wonted.cn/www-minijuejos/index.html www minijuejos com] [http://bachecaperadulti.romanikki.cn/index.html bacheca per adulti] [http://snailtail.cn/www-pieropelu/index.html www pieropelu com] [http://kitmun.cn/you-coul.html you coul make a killing] [http://kitmun.cn/yello.html yello] [http://kitmun.cn/zucchero-zu.html zucchero zu] [http://quoits.cn/addio-ultimo.html addio ultimo uomo] [http://quoits.cn/actas-de.html actas de marusia - storia di un massacro] [http://snailtail.cn/www-sesso-orale/index.html www sesso orale com] [http://wonted.cn/www-netscape/index.html www netscape com] [http://wonted.cn/www-alpieagles/index.html www alpieagles] [http://snailtail.cn/yair/index.html yair] [http://brizziendco.midways.cn/index.html brizzi end co] [http://badgirlsex.clung.cn/index.html badgirlsex] [http://snailtail.cn/xitrum-com/index.html xitrum com] [http://kitmun.cn/yaooh-meteo.html yaooh meteo] [http://knock-knock.cn/www-immaggini/index.html www immaggini decapitazioni it] [http://knock-knock.cn/www-clubamerica/index.html www clubamerica com] [http://bassethound.clung.cn/index.html basset hound] [http://baraleclaudia.fast-road.cn/index.html barale claudia] [http://bancaunicreditit.romanikki.cn/index.html bancaunicredit it] [http://britishmuseummostre.kittiss.cn/index.html british museum mostre] [http://bennybenasisatisfaction.midways.cn/index.html benny benasi satisfaction] [http://bluecurtianfalls.midways.cn/index.html blue curtian falls] [http://baffanculo.clung.cn/index.html baffanculo] [http://kitmun.cn/zeetv.html zeetv] [http://wonted.cn/www-assistente/index.html www assistente tecnico it] {{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
'''Co
2000
mpiler:''' [[Visual C sharp|Visual C#]] 2005
 
using System;
Line 157 ⟶ 155:
}
 
==[[C plus plus|C ++]]==
[[Category:C plus plus]]
'''Compiler:''' [[GNU Compiler Collection]] 4.1.1
Line 173 ⟶ 171:
int ary[]={1,2,3,4,5};
//stl for_each
std::for_each(ary,ary +5,print_square);
return 0;
}
Line 232 ⟶ 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 269 ⟶ 267:
 
(defvar *a* (vector 1 2 3))
(map-into *a* #'1 + *a*)
 
==[[E]]==
Line 303 ⟶ 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 334 ⟶ 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
Cookies help us deliver our services. By using our services, you agree to our use of cookies.