Apply a callback to an array: Difference between revisions

(Added Odin variant)
(23 intermediate revisions by 15 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 797 ⟶ 811:
 
=={{header|COBOL}}==
{{Works with|COBOL 2002}}
 
Basic implementation of a map function:
<syntaxhighlight lang="cobolcobolfree"> IDENTIFICATION>>SOURCE FORMAT IS DIVISION.FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. Map.
PROGRAM-ID. map.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Table-Size CONSTANT 30.
 
LOCAL-STORAGE SECTION.
01 I USAGE UNSIGNED-INT.
 
LINKAGE SECTION.
01 Table-Param.
03 Table-Values USAGE COMP-2 OCCURS Table-Size TIMES.
 
DATA DIVISION.
01 Func-Id PIC X(30).
LOCAL-STORAGE SECTION.
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-ptr USAGE IS PROGRAM-POINTER.
 
PROCEDURE DIVISION USING BY REFERENCE table-param, BY VALUE func-ptr.
PROCEDURE DIVISION USING Table-Param Func-Id.
PERFORM VARYING Ii FROM 1 BY 1 UNTIL Table-Sizei IS <GREATER ITHAN table-size
CALL Funcfunc-Idptr USING BY REFERENCE Tabletable-Values values(Ii)
END-PERFORM
GOBACK.
 
END PROGRAM map.</syntaxhighlight>
GOBACK
.</syntaxhighlight>
 
=={{header|CoffeeScript}}==
Line 1,116 ⟶ 1,127:
 
=={{header|Elena}}==
ELENA 56.0x :
<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:(PrintSecondPower)
}</syntaxhighlight>
 
Line 1,328 ⟶ 1,339:
----------------------------------------
Press any key to continue...</pre>
 
=={{header|Fe}}==
<syntaxhighlight lang="clojure">
(= map (fn (f lst)
(let res (cons nil nil))
(let tail res)
(while lst
(setcdr tail (cons (f (car lst)) nil))
(= lst (cdr lst))
(= tail (cdr tail)))
(cdr res)))
 
(print (map (fn (x) (* x x)) '(1 2 3 4 5 6 7 8 9 10)))
</syntaxhighlight>
 
=={{header|Forth}}==
Line 1,524 ⟶ 1,549:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Apply_a_callback_to_an_array}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
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:
In '''[https://formulae.org/?example=Apply_a_callback_to_an_array this]''' page you can see the program(s) related to this task and their results.
 
[[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,653 ⟶ 1,688:
{{Out}}
<pre>array (1,10) [(1,1),(2,4),(3,9),(4,16),(5,25),(6,36),(7,49),(8,64),(9,81),(10,100)]</pre>
 
=={{header|Guish}}==
{{works with|guish|2.5.1}}
<syntaxhighlight lang="guish">
# applies add2 (adds 2) to each element
add2 = {
return add(@1, 2)
}
l = {1, 2, 3, 4, 5, 6, 7}
puts each(add2, flat(@l))
</syntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 1,672 ⟶ 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,689 ⟶ 1,752:
 
But note that this is a trivial example since <code>*: 1 2 3 4 5</code> would get the same result. Then again, this is something of a trivial exercise in J since all of J is designed around the idea of applying functions usefully to arrays.
 
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn map<T, U>(anon array: [T], function: fn(anon x: T) -> U) throws -> [U] {
mut result: [U] = []
result.ensure_capacity(array.size())
for item in array {
result.push(value: function(item))
}
return result
}
 
fn main() {
let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let array_squared = map(array, function: fn(anon n: i64) => n * n)
println("{}", array_squared)
}
</syntaxhighlight>
 
{{out}}
<pre>
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
</pre>
 
=={{header|Java}}==
Line 1,860 ⟶ 1,946:
-> [1,4,9,16,25,36,49,64,81,100]
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
&arr = fn.arrayGenerateFrom(fn.inc, 10)
fn.println(&arr)
fn.arrayMap(&arr, fn.combC(fn.pow, 2))
fn.println(&arr)
</syntaxhighlight>
{{out}}
<pre>
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
</pre>
 
=={{header|Lang5}}==
Line 1,867 ⟶ 1,966:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">writeln map ffn{^2}, 1..10</syntaxhighlight>
 
{{out}}
Line 2,174 ⟶ 2,273:
=={{header|Nim}}==
 
<syntaxhighlight lang="nim">var arr = @[1,2,3,4]
from std/sequtils import apply
arr.apply proc(some: var int) = echo(some, " squared = ", some*some)</syntaxhighlight>
let arr = @[1,2,3,4]
arr.apply proc(some: int) = echo(some, " squared = ", some*some)</syntaxhighlight>
 
{{Out}}
Line 2,182 ⟶ 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,082 ⟶ 3,196:
}
</syntaxhighlight>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ → array func
≪ array 0 CON
1 array SIZE FOR j
j array j GET func EVAL PUT
NEXT
≫ ≫
´MAP’ STO
 
[1,2,3,4,5,6,7,8,9] ≪ SQ ≫ MAP
{{out}}
<pre>
1: [ 1 4 9 16 25 36 49 64 81 ]
</pre>
 
=={{header|Ruby}}==
Line 3,840 ⟶ 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