Apply a callback to an array: Difference between revisions
→{{header|langur}}
Litlighilit (talk | contribs) (→Nim: Add necessary `import` for `apply`) |
Langurmonkey (talk | contribs) |
||
(13 intermediate revisions by 10 users not shown) | |||
Line 440:
2.23606798
</pre>
=={{header|Binary Lambda Calculus}}==
In the lambda calculus, we can map over a list as in https://github.com/tromp/AIT/blob/master/lists/map.lam, which gives the following BLC program to negate every bit of input:
<pre>010001101000000101100000000001011000000101111111010110010111111101111110111010</pre>
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Square ← ט
array ← 2‿3‿5‿7‿11‿13
The use of the ¨ modifier is the general approach, but actually not necessary with arithmetic functions.
{{out}}
<pre>⟨ 4 9 25 49 121 169 ⟩</pre>
=={{header|Bracmat}}==
Line 797 ⟶ 811:
=={{header|COBOL}}==
{{Works with|COBOL 2002}}
Basic implementation of a map function:
<syntaxhighlight lang="
▲ DATA DIVISION.
WORKING-STORAGE SECTION.▼
01 Table-Size CONSTANT 30.▼
LINKAGE SECTION.▼
DATA DIVISION.
01 i USAGE IS INDEX.
01 table-param.
03 table-values USAGE IS FLOAT-LONG, OCCURS table-size TIMES.
01 func-ptr USAGE IS PROGRAM-POINTER.
PROCEDURE DIVISION USING BY REFERENCE table-param, BY VALUE func-ptr.
END PROGRAM map.</syntaxhighlight>
▲ GOBACK
▲ .</syntaxhighlight>
=={{header|CoffeeScript}}==
Line 1,116 ⟶ 1,127:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import system'routines;
Line 1,123 ⟶ 1,134:
public program()
{
new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.forEach
}</syntaxhighlight>
Line 1,538 ⟶ 1,549:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Apply_a_callback_to_an_array}}
'''Solution'''
Most programming languages define a high-order map function. In Fōrmulæ, there is ''arraization'' (by analogy with ''summation''). In the following expression, the "big" curly braces resembles the "big" sigma of a summation:
[[File:Fōrmulæ - Apply a callback to an array 01.png]]
[[File:Fōrmulæ - Apply a callback to an array 02.png]]
The elements of the array are not required to be of the same type:
[[File:Fōrmulæ - Apply a callback to an array 03.png]]
[[File:Fōrmulæ - Apply a callback to an array 04.png]]
=={{header|GAP}}==
Line 1,697 ⟶ 1,718:
will yield a scalar if <tt>a</tt> is scalar or a vector if <tt>a</tt> is a vector or an n-dimensional array if <tt>a</tt> is an n-dimensional array
=={{header|Insitux}}==
<syntaxhighlight lang="insitux">; apply a named function
(map inc [1 2 3 4])</syntaxhighlight>
<syntaxhighlight lang="insitux">; apply a parameterised closure
(map (fn x (+ x 1)) [1 2 3 4])</syntaxhighlight>
<syntaxhighlight lang="insitux">; apply a non-parameterised closure
(map #(+ % 1) [1 2 3 4])</syntaxhighlight>
<syntaxhighlight lang="insitux">; apply an explicit partial closure
(map @(+ 1) [1 2 3 4])</syntaxhighlight>
<syntaxhighlight lang="insitux">; apply an implicit partial closure
(map (+ 1) [1 2 3 4])</syntaxhighlight>
=={{header|Io}}==
Line 1,928 ⟶ 1,966:
=={{header|langur}}==
<syntaxhighlight lang="langur">writeln map
{{out}}
Line 2,237 ⟶ 2,275:
<syntaxhighlight lang="nim">
from std/sequtils import apply
arr.apply proc(some:
{{Out}}
Line 2,245 ⟶ 2,283:
3 squared = 9
4 squared = 16
=={{header|Nutt}}==
<syntaxhighlight lang="Nutt">
module main
imports native.io.output.say
operator |> (arr:Array,f:Procedure):Array==>{f(x) of x |-> arr}
say({0,1,2,3,4,5}|>a==>a+2)//|{2,3,4,5,6,7}
end
</syntaxhighlight>
=={{header|Oberon-2}}==
Line 3,919 ⟶ 3,970:
=={{header|Wren}}==
<syntaxhighlight lang="
arr = arr.map { |x| x * 2 }.toList
arr = arr.map(Fn.new { |x| x / 2 }).toList
|