Higher-order functions: Difference between revisions

Add Julia implementation of Higher-order functions
m (Added Raven code for Higher-order functions)
(Add Julia implementation of Higher-order functions)
Line 1,103:
<lang joy>2 3 [first] second.</lang>
The program prints 6.
 
=={{header|Julia}}==
 
<lang Julia>
function foo(x)
str = x("world")
println("hello $(str)!")
end
foo(y -> "blue $y") # prints "hello blue world"
</lang>
 
The above code snippet defines a named function, <tt>foo</tt>, which takes a single argument, which is a <tt>Function</tt>.
<tt>foo</tt> calls this function on the string literal <tt>"world"</tt>, and then interpolates the result into the "hello ___!" string literal, and prints it.
In the final line, <tt>foo</tt> is called with an anonymous function that takes a string, and returns a that string with <tt>"blue "</tt> preppended to it.
 
<lang Julia>
function g(x,y,z)
x(y,z)
end
println(g(+,2,3)) # prints 5
</lang>
 
This code snippet defines a named function <tt>g</tt> that takes three arguments: <tt>x</tt> is a function to call, and <tt>y</tt> and <tt>z</tt> are the values to call <tt>x</tt> on.
We then call <tt>g</tt> on the <tt>+</tt> function. Operators in Julia are just special names for functions.
 
{{omit from|Liberty BASIC}}