Apply a callback to an array: Difference between revisions

m
Restored alphabetical order of languages
(→‎{{header|Vorpal}}: Added Vim Script)
m (Restored alphabetical order of languages)
Line 456:
<lang clean>Start :: {#Int}
Start = mapArray square values</lang>
 
=={{header|Clojure}}==
 
<lang lisp>;; apply a named function, inc
(map inc [1 2 3 4])</lang>
 
<lang lisp>;; apply a function
(map (fn [x] (* x x)) [1 2 3 4])</lang>
 
<lang lisp>;; shortcut syntax for a function
(map #(* % %) [1 2 3 4])</lang>
 
=={{header|COBOL}}==
Line 508 ⟶ 519:
<lang lisp>(defvar *a* (vector 1 2 3))
(map-into *a* #'1+ *a*)</lang>
 
=={{header|Clojure}}==
 
<lang lisp>;; apply a named function, inc
(map inc [1 2 3 4])</lang>
 
<lang lisp>;; apply a function
(map (fn [x] (* x x)) [1 2 3 4])</lang>
 
<lang lisp>;; shortcut syntax for a function
(map #(* % %) [1 2 3 4])</lang>
 
=={{header|Component Pascal}}==
Line 794:
This is also "Example 2" in the Euphoria documentation for <code>routine_id()</code>.
Note that this example will not work for multi-dimensional sequences.
 
=={{header|Factor}}==
Print each element squared:
<lang factor>{ 1 2 3 4 } [ sq . ] each</lang>
 
Collect return values:
<lang factor>{ 1 2 3 4 } [ sq ] map</lang>
 
=={{header|Fantom}}==
 
In Fantom, functions can be passed to a collection iterator, such as 'each'. 'map' is used similarly, and the results are collected into a list.
 
<lang fantom>
class Main
{
public static Void main ()
{
[1,2,3,4,5].each |Int i| { echo (i) }
Int[] result := [1,2,3,4,5].map |Int i->Int| { return i * i }
echo (result)
}
}
</lang>
 
Output:
<pre>
1
2
3
4
5
[1, 4, 9, 16, 25]
</pre>
 
=={{header|FBSL}}==
Line 859 ⟶ 892:
----------------------------------------
Press any key to continue...</pre>
 
=={{header|Factor}}==
Print each element squared:
<lang factor>{ 1 2 3 4 } [ sq . ] each</lang>
 
Collect return values:
<lang factor>{ 1 2 3 4 } [ sq ] map</lang>
 
=={{header|Fantom}}==
 
In Fantom, functions can be passed to a collection iterator, such as 'each'. 'map' is used similarly, and the results are collected into a list.
 
<lang fantom>
class Main
{
public static Void main ()
{
[1,2,3,4,5].each |Int i| { echo (i) }
Int[] result := [1,2,3,4,5].map |Int i->Int| { return i * i }
echo (result)
}
}
</lang>
 
Output:
<pre>
1
2
3
4
5
[1, 4, 9, 16, 25]
</pre>
 
=={{header|Forth}}==
Line 1,174:
 
<pre>[1, 4, 9, 16, 25]</pre>
 
=={{header|TIScript}}==
 
JavaScript alike:
 
<lang javascript>var a = [1, 2, 3, 4, 5];
a.map(function(v) { return v * v; })
</lang>
 
Using short form of lambda notation:
<lang javascript>var a = [1, 2, 3, 4, 5];
a.map( :v: v*v );
</lang>
 
=={{header|Joy}}==
Line 1,280 ⟶ 1,267:
However, remember that hash table do not have an implicit ordering on their elements, like arrays do,
so pairs() is not guaranteed to return the elements in the same order as ipairs()
 
=={{header|Nimrod}}==
 
<lang nimrod>var arr = [1,2,3,4]
arr.map proc(some: var int) = echo(some, " squared = ", some*some)</lang>
 
''' Output
1 squared = 1
2 squared = 4
3 squared = 9
4 squared = 16
 
=={{header|M4}}==
Line 1,466 ⟶ 1,442:
<lang nial>each (* [first, first] ) 1 2 3 4
=1 4 9 16</lang>
 
=={{header|Nimrod}}==
 
<lang nimrod>var arr = [1,2,3,4]
arr.map proc(some: var int) = echo(some, " squared = ", some*some)</lang>
 
{{Out}}
1 squared = 1
2 squared = 4
3 squared = 9
4 squared = 16
 
=={{header|Objeck}}==
Line 2,252 ⟶ 2,239:
<math>5</math><br>
<math>\begin{Bmatrix}1 & \sqrt{2} & \sqrt{3} & 2 & \sqrt{5}\end{Bmatrix}</math>
 
=={{header|TIScript}}==
 
JavaScript alike:
 
<lang javascript>var a = [1, 2, 3, 4, 5];
a.map(function(v) { return v * v; })
</lang>
 
Using short form of lambda notation:
<lang javascript>var a = [1, 2, 3, 4, 5];
a.map( :v: v*v );
</lang>
 
=={{header|Toka}}==
Line 2,272:
( Add 1 to each item in the array )
a 5 [ 1 + ] map-array</lang>
 
=={{header|TXR}}==
 
Print 1 through 10 out of a vector, using <code>prinl</code> the callback, right from the system shell command prompt:
 
<lang bash>$ txr -e '[mapcar prinl #(1 2 3 4 5 6 7 8 9 10)]'
1
2
3
4
5
6
7
8
9
10</lang>
 
TXR extends traditional Lisp list processing primitives to work with vectors and strings also, which is why <code>mapcar</code> cheerfully traverses a vector.
 
=={{header|TorqueScript}}==
Line 2,331 ⟶ 2,313:
=> How are you?
</lang>
 
=={{header|TXR}}==
 
Print 1 through 10 out of a vector, using <code>prinl</code> the callback, right from the system shell command prompt:
 
<lang bash>$ txr -e '[mapcar prinl #(1 2 3 4 5 6 7 8 9 10)]'
1
2
3
4
5
6
7
8
9
10</lang>
 
TXR extends traditional Lisp list processing primitives to work with vectors and strings also, which is why <code>mapcar</code> cheerfully traverses a vector.
 
=={{header|UNIX Shell}}==