Apply a callback to an array: Difference between revisions

imported>Acediast
(→‎{{header|COBOL}}: fixed to standard USAGEs)
(7 intermediate revisions by 7 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
 
Square¨ array</syntaxhighlight>
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 799 ⟶ 813:
{{Works with|COBOL 2002}}
Basic implementation of a map function:
<syntaxhighlight lang="cobolcobolfree"> IDENTIFICATION>>SOURCE FORMAT IS DIVISION.FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. map.
 
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 i 01 i USAGE IS INDEX.
01 table-size CONSTANT AS 30.
LINKAGE SECTION.
01 table-param.
03 table-values USAGE IS FLOAT-LONG, OCCURS table-size TIMES.
01 func-idptr PICTURE USAGE IS X(30)PROGRAM-POINTER.
 
PROCEDURE DIVISION USING BY REFERENCE table-param, BY VALUE func-idptr.
PERFORM VARYING i FROM 1 BY 1 UNTIL i IS GREATER THAN table-size
CALL func-idptr USING BY REFERENCE table-values(i)
END-PERFORM
GOBACK.
 
END PROGRAM map.</syntaxhighlight>
 
=={{header|CoffeeScript}}==
Line 1,112 ⟶ 1,127:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
 
Line 1,119 ⟶ 1,134:
public program()
{
new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.forEach:(PrintSecondPower)
}</syntaxhighlight>
 
Line 1,538 ⟶ 1,553:
'''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 resembleresembles the "big" sigma of a summation:
 
[[File:Fōrmulæ - Apply a callback to an array 01.png]]
Line 1,951 ⟶ 1,966:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">writeln map ffn{^2}, 1..10</syntaxhighlight>
 
{{out}}
Line 3,955 ⟶ 3,970:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var arr = [1, 2, 3, 4, 5]
arr = arr.map { |x| x * 2 }.toList
arr = arr.map(Fn.new { |x| x / 2 }).toList
885

edits