Higher-order functions: Difference between revisions
Content added Content deleted
Line 980: | Line 980: | ||
{{out}} |
{{out}} |
||
<pre>second</pre> |
<pre>second</pre> |
||
=={{header|Elixir}}== |
|||
<lang elixir>iex(1)> defmodule RC do |
|||
...(1)> def first(f), do: f.() |
|||
...(1)> def second, do: :hello |
|||
...(1)> end |
|||
{:module, RC, |
|||
<<70, 79, 82, 49, 0, 0, 4, 224, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 142, |
|||
131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95 |
|||
, 118, 49, 108, 0, 0, 0, 2, 104, 2, ...>>, |
|||
{:second, 0}} |
|||
iex(2)> RC.first(fn -> RC.second end) |
|||
:hello |
|||
iex(3)> RC.first(&RC.second/0) # Another expression |
|||
:hello |
|||
iex(4)> f = fn -> :world end # Anonymous function |
|||
#Function<20.54118792/0 in :erl_eval.expr/5> |
|||
iex(5)> RC.first(f) |
|||
:world</lang> |
|||
=={{header|Erlang}}== |
|||
Erlang functions are atoms, and they're considered different functions if their arity (the number of arguments they take) is different. As such, an Erlang function must be passed as <code>fun Function/Arity</code>, but can be used as any other variable: |
|||
<lang erlang>-module(test). |
|||
-export([first/1, second/0]). |
|||
first(F) -> F(). |
|||
second() -> hello.</lang> |
|||
Testing it: |
|||
<lang erlang>1> c(tests). |
|||
{ok, tests} |
|||
2> tests:first(fun tests:second/0). |
|||
hello |
|||
3> tests:first(fun() -> anonymous_function end). |
|||
anonymous_function</lang> |
|||
=={{header|ERRE}}== |
|||
ERRE function are limited to one-line FUNCTION, but you can write: |
|||
<lang ERRE> |
|||
PROGRAM FUNC_PASS |
|||
FUNCTION ONE(X,Y) |
|||
ONE=(X+Y)^2 |
|||
END FUNCTION |
|||
FUNCTION TWO(X,Y) |
|||
TWO=ONE(X,Y)+1 |
|||
END FUNCTION |
|||
BEGIN |
|||
PRINT(TWO(10,11)) |
|||
END PROGRAM |
|||
</lang> |
|||
Answer is 442 |
|||
=={{header|Euler Math Toolbox}}== |
|||
<lang Euler Math Toolbox> |
|||
>function f(x,a) := x^a-a^x |
|||
>function dof (f$:string,x) := f$(x,args()); |
|||
>dof("f",1:5;2) |
|||
[ -1 0 1 0 -7 ] |
|||
>plot2d("f",1,5;2): |
|||
</lang> |
|||
=={{header|Euphoria}}== |
|||
<lang euphoria>procedure use(integer fi, integer a, integer b) |
|||
print(1,call_func(fi,{a,b})) |
|||
end procedure |
|||
function add(integer a, integer b) |
|||
return a + b |
|||
end function |
|||
use(routine_id("add"),23,45)</lang> |
|||
=={{header|F Sharp|F#}}== |
|||
We define a function that takes another function f as an argument and applies that function twice to the argument x: |
|||
<lang fsharp>> let twice f x = f (f x);; |
|||
val twice : ('a -> 'a) -> 'a -> 'a |
|||
> twice System.Math.Sqrt 81.0;; |
|||
val it : float = 3.0</lang> |
|||
Another example, using an operator as a function: |
|||
<lang fsharp>> List.map2 (+) [1;2;3] [3;2;1];; |
|||
val it : int list = [4; 4; 4]</lang> |
|||
=={{header|Factor}}== |
|||
Using words (factor's functions) : |
|||
<lang factor>USING: io ; |
|||
IN: rosetacode |
|||
: argument-function1 ( -- ) "Hello World!" print ; |
|||
: argument-function2 ( -- ) "Goodbye World!" print ; |
|||
! normal words have to know the stack effect of the input parameters they execute |
|||
: calling-function1 ( another-function -- ) execute( -- ) ; |
|||
! unlike normal words, inline words do not have to know the stack effect. |
|||
: calling-function2 ( another-function -- ) execute ; inline |
|||
! Stack effect has to be written for runtime computed values : |
|||
: calling-function3 ( bool -- ) \ argument-function1 \ argument-function2 ? execute( -- ) ; |
|||
</lang> |
|||
( scratchpad ) |
|||
\ argument-function1 calling-function1 |
|||
\ argument-function1 calling-function2 |
|||
t calling-function3 |
|||
f calling-function3 |
|||
Hello World! |
|||
Hello World! |
|||
Hello World! |
|||
Goodbye World! |
|||
=={{header|FALSE}}== |
|||
Anonymous code blocks are the basis of FALSE control flow and function definition. These blocks may be passed on the stack as with any other parameter. |
|||
<lang false>[f:[$0>][@@\f;!\1-]#%]r: { reduce n stack items using the given basis and binary function } |
|||
1 2 3 4 0 4[+]r;!." " { 10 } |
|||
1 2 3 4 1 4[*]r;!." " { 24 } |
|||
1 2 3 4 0 4[$*+]r;!. { 30 }</lang> |
|||
=={{header|Fantom}}== |
|||
<lang fantom> |
|||
class Main |
|||
{ |
|||
// apply given function to two arguments |
|||
static Int performOp (Int arg1, Int arg2, |Int, Int -> Int| fn) |
|||
{ |
|||
fn (arg1, arg2) |
|||
} |
|||
public static Void main () |
|||
{ |
|||
echo (performOp (2, 5, |Int a, Int b -> Int| { a + b })) |
|||
echo (performOp (2, 5, |Int a, Int b -> Int| { a * b })) |
|||
} |
|||
} |
|||
</lang> |
|||
=={{header|Fōrmulæ}}== |
|||
In [http://dictionary.formulae.org/Higher-order_functions 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. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— 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}}== |
|||
Forth words can be referenced on the stack via their ''execution token'' or XT. An XT is obtained from a word via the quote operator, and invoked via '''EXECUTE'''. Anonymous functions may be defined via ''':NONAME''' (returning an XT) instead of a standard colon definition. |
|||
<lang forth>: square dup * ; |
|||
: cube dup dup * * ; |
|||
: map. ( xt addr len -- ) |
|||
0 do 2dup i cells + @ swap execute . loop 2drop ; |
|||
create array 1 , 2 , 3 , 4 , 5 , |
|||
' square array 5 map. cr \ 1 4 9 16 25 |
|||
' cube array 5 map. cr \ 1 8 27 64 125 |
|||
:noname 2* 1+ ; array 5 map. cr \ 3 5 7 9 11</lang> |
|||
=={{header|Fortran}}== |
|||
{{works with|Fortran|90 and later}} |
|||
use the EXTERNAL attribute to show the dummy argument is another function rather than a data object. i.e. |
|||
<lang fortran>FUNCTION FUNC3(FUNC1, FUNC2, x, y) |
|||
REAL, EXTERNAL :: FUNC1, FUNC2 |
|||
REAL :: FUNC3 |
|||
REAL :: x, y |
|||
FUNC3 = FUNC1(x) * FUNC2(y) |
|||
END FUNCTION FUNC3</lang> |
|||
Another way is to put the functions you want to pass in a module: |
|||
<lang fortran>module FuncContainer |
|||
implicit none |
|||
contains |
|||
function func1(x) |
|||
real :: func1 |
|||
real, intent(in) :: x |
|||
func1 = x**2.0 |
|||
end function func1 |
|||
function func2(x) |
|||
real :: func2 |
|||
real, intent(in) :: x |
|||
func2 = x**2.05 |
|||
end function func2 |
|||
end module FuncContainer |
|||
program FuncArg |
|||
use FuncContainer |
|||
implicit none |
|||
print *, "Func1" |
|||
call asubroutine(func1) |
|||
print *, "Func2" |
|||
call asubroutine(func2) |
|||
contains |
|||
subroutine asubroutine(f) |
|||
! the following interface is redundant: can be omitted |
|||
interface |
|||
function f(x) |
|||
real, intent(in) :: x |
|||
real :: f |
|||
end function f |
|||
end interface |
|||
real :: px |
|||
px = 0.0 |
|||
do while( px < 10.0 ) |
|||
print *, px, f(px) |
|||
px = px + 1.0 |
|||
end do |
|||
end subroutine asubroutine |
|||
end program FuncArg</lang> |
|||
=={{header|FreeBASIC}}== |
|||
<lang freebasic>' FB 1.05.0 Win64 |
|||
Function square(n As Integer) As Integer |
|||
Return n * n |
|||
End Function |
|||
Function cube(n As Integer) As Integer |
|||
Return n * n * n |
|||
End Function |
|||
Sub doCalcs(from As Integer, upTo As Integer, title As String, func As Function(As Integer) As Integer) |
|||
Print title; " -> "; |
|||
For i As Integer = from To upTo |
|||
Print Using "#####"; func(i); |
|||
Next |
|||
Print |
|||
End Sub |
|||
doCalcs 1, 10, "Squares", @square |
|||
doCalcs 1, 10, "Cubes ", @cube |
|||
Print |
|||
Print "Press any key to quit" |
|||
Sleep</lang> |
|||
{{out}} |
|||
<pre> |
|||
Squares -> 1 4 9 16 25 36 49 64 81 100 |
|||
Cubes -> 1 8 27 64 125 216 343 512 729 1000 |
|||
</pre> |
|||
=={{header|Frink}}== |
=={{header|Frink}}== |