Apply a callback to an array: Difference between revisions

Content added Content deleted
(add SenseTalk examples)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 674: Line 674:
console.log map arr, f # prints [1, 4, 9, 16, 25]
console.log map arr, f # prints [1, 4, 9, 16, 25]
</lang>
</lang>



=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 807: Line 806:
end.
end.
</lang>
</lang>
=={{header|Déjà Vu}}==
There is a <code>map</code> builtin that does just this.
<lang dejavu>!. map @++ [ 1 4 8 ]

#implemented roughly like this:
#map f lst:
# ]
# for i in lst:
# f i
# [</lang>
{{out}}
<pre>[ 2 5 9 ]</pre>


=={{header|Dyalect}}==
=={{header|Dyalect}}==
Line 832: Line 819:
print(squares)</lang>
print(squares)</lang>

=={{header|Déjà Vu}}==
There is a <code>map</code> builtin that does just this.
<lang dejavu>!. map @++ [ 1 4 8 ]

#implemented roughly like this:
#map f lst:
# ]
# for i in lst:
# f i
# [</lang>
{{out}}
<pre>[ 2 5 9 ]</pre>


=={{header|E}}==
=={{header|E}}==
Line 983: Line 983:
6
6
</lang>
</lang>



=={{header|ERRE}}==
=={{header|ERRE}}==
Line 1,039: Line 1,038:
This is also "Example 2" in the Euphoria documentation for <code>routine_id()</code>.
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.
Note that this example will not work for multi-dimensional sequences.

=={{header|F_Sharp|F#}}==
Apply a named function to each member of the array. The result is a new array of the same size as the input.
<lang fsharp>let evenp x = x % 2 = 0
let result = Array.map evenp [| 1; 2; 3; 4; 5; 6 |]</lang>
The same can be done using anonymous functions, this time squaring the members of the input array.
<lang fsharp>let result = Array.map (fun x -> x * x) [|1; 2; 3; 4; 5|]</lang>
Use ''iter'' if the applied function does not return a value.
<lang fsharp>Array.iter (fun x -> printfn "%d" x) [|1; 2; 3; 4; 5|]</lang>


=={{header|Factor}}==
=={{header|Factor}}==
Line 1,138: Line 1,146:
----------------------------------------
----------------------------------------
Press any key to continue...</pre>
Press any key to continue...</pre>

=={{header|Fōrmulæ}}==

In [http://wiki.formulae.org/Apply_a_callback_to_an_array this] page you can see the solution of this task.

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). 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 transportation effects more than visualization and edition.

The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.


=={{header|Forth}}==
=={{header|Forth}}==
Line 1,253: Line 1,253:
println[map[f, a]]
println[map[f, a]]
</lang>
</lang>

=={{header|F_Sharp|F#}}==
Apply a named function to each member of the array. The result is a new array of the same size as the input.
<lang fsharp>let evenp x = x % 2 = 0
let result = Array.map evenp [| 1; 2; 3; 4; 5; 6 |]</lang>
The same can be done using anonymous functions, this time squaring the members of the input array.
<lang fsharp>let result = Array.map (fun x -> x * x) [|1; 2; 3; 4; 5|]</lang>
Use ''iter'' if the applied function does not return a value.
<lang fsharp>Array.iter (fun x -> printfn "%d" x) [|1; 2; 3; 4; 5|]</lang>


=={{header|FunL}}==
=={{header|FunL}}==
Line 1,291: Line 1,282:
map (+1) [1,2,3] -- [2,3,4]
map (+1) [1,2,3] -- [2,3,4]
</lang>
</lang>

=={{header|Fōrmulæ}}==

In [http://wiki.formulae.org/Apply_a_callback_to_an_array this] page you can see the solution of this task.

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). 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 transportation effects more than visualization and edition.

The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.


=={{header|GAP}}==
=={{header|GAP}}==
Line 2,199: Line 2,198:
# filter an array
# filter an array
my @e = grep { $_ % 2 == 0 } @a; # @e is now (2, 4)</lang>
my @e = grep { $_ % 2 == 0 } @a; # @e is now (2, 4)</lang>

=={{header|Perl 6}}==
{{works with|Rakudo|2015.10-11}}

<lang perl6>sub function { 2 * $^x + 3 };
my @array = 1 .. 5;

# via map function
.say for map &function, @array;

# via map method
.say for @array.map(&function);

# via for loop
for @array {
say function($_);
}

# via the "hyper" metaoperator and method indirection
say @array».&function;

# we neither need a variable for the array nor for the function
say [1,2,3]>>.&({ $^x + 1});
</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,513: Line 2,488:
;; the usual functional `map'
;; the usual functional `map'
(vector-map sqr #(1 2 3 4 5))
(vector-map sqr #(1 2 3 4 5))
</lang>

=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.10-11}}

<lang perl6>sub function { 2 * $^x + 3 };
my @array = 1 .. 5;

# via map function
.say for map &function, @array;

# via map method
.say for @array.map(&function);

# via for loop
for @array {
say function($_);
}

# via the "hyper" metaoperator and method indirection
say @array».&function;

# we neither need a variable for the array nor for the function
say [1,2,3]>>.&({ $^x + 1});
</lang>
</lang>


Line 2,633: Line 2,633:
after b.10=3628800
after b.10=3628800
</pre>
</pre>

=={{header|Ring}}==
<lang ring>
for x in [1,2,3,4,5]
x = x*x
next
</lang>


=={{header|RLaB}}==
=={{header|RLaB}}==
Line 2,689: Line 2,696:
y.[i] = sin( x.[i] );
y.[i] = sin( x.[i] );
}
}
</lang>

=={{header|Ring}}==
<lang ring>
for x in [1,2,3,4,5]
x = x*x
next
</lang>
</lang>


Line 2,833: Line 2,833:
For creating a new array, we can use the Array.map method:
For creating a new array, we can use the Array.map method:
<lang ruby>[1,2,3,4,5].map{|i| i**2 }</lang>
<lang ruby>[1,2,3,4,5].map{|i| i**2 }</lang>

=={{header|Simula}}==
=={{header|Simula}}==
<lang simula>BEGIN
<lang simula>BEGIN
Line 3,241: Line 3,242:


0 OK, 0:514</pre>
0 OK, 0:514</pre>

=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
{{works with|Bourne Shell}}
{{works with|Bourne Shell}}
Line 3,283: Line 3,285:
apply squaring (dup *) to each member of collection
apply squaring (dup *) to each member of collection
<lang v>[1 2 3 4] [dup *] map</lang>
<lang v>[1 2 3 4] [dup *] map</lang>

=={{header|VBA}}==
=={{header|VBA}}==
<lang vb>
<lang vb>