Hi,
based on this
f = x -> x^2
f(10)
I’d like to develop a function with two inputs
f1(a,b) = a,b -> a^2+b
f1(2,1)
but this returns (2, λ)
. What are the meaning of lambda returned here and the right syntax for this?
Hi,
based on this
f = x -> x^2
f(10)
I’d like to develop a function with two inputs
f1(a,b) = a,b -> a^2+b
f1(2,1)
but this returns (2, λ)
. What are the meaning of lambda returned here and the right syntax for this?
λ refers to the anonymous function b->a^2+b
Simply do
f1(a,b) = a^2+b
This also works if you want an anonymous function rather than a generic function.
julia> f1 = (a, b) -> a^2 + b
#3 (generic function with 1 method)
julia> f1(2, 1)
5
Oh I see
julia> f1(a,b) = a^2 + b
f1 (generic function with 1 method)julia> f2 = (a, b) → a^2 + b
#44 (generic function with 1 method)
But what is anonymous function used for? This seems not to be explained in its documentation
https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions-1
Just a little explanation, if you are curious. The code gets interpeted as
f1(a, b) = a, (b -> a^2+b)
which gets interpreted as
f1(a, b) = return a, (b -> a^2+b)
which produces the obvious error.
“Anonymous” literally means “without a name” and you use them whenever your function might not merit being named. For example, if I want to add 1.3 to each item of a list, defining add1point3(x)
might seem a bit silly. So I can instead say map(x -> x + 1.3, list)
and avoiding giving that operation a special name.
If you have a function that takes a function as an argument, it can more concise to define an anonymous function right in the outer function argument list.
A trivial example:
julia> d = rand(10)
10-element Array{Float64,1}:
0.3551247514375906
0.3340804663050978
0.6544044171195078
0.9508829086833299
0.6444864931672418
0.3549102821541359
0.3284450308572071
0.8639674871803269
0.1763711097796281
0.32769620811874867
julia> findfirst(x -> x>0.5, d)
3
There may be an implicit doubt in @1634’s question that remains to be answered: Ok, anonymous functions can be used as “throw-away functions” in arguments for map
, findfirst
, etc., without ever giving them a name. But what’s the difference between a “named” function and an anonymous one when it is (so to speak) “given” a name, i.e. assigned to a variable?
There are two notetworthy differences that I know of:
const
, whereas function names are constant by definition. For instance:julia> foo(x) = x.^2
foo (generic function with 1 method)
julia> foo(3)
9
julia> foo = 0
ERROR: invalid redefinition of constant foo
[...]
julia> bar = x -> x.^2
#7 (generic function with 1 method)
julia> bar(3)
9
julia> bar = 0
0
There is no explicit syntax AFAIK, but one could easily get around this by defining a macro that converts
@multianon begin
(a::Int, b::Int) = a + b
(a::Float64, b::Float64) = a * b
end
to
let
f(a::Int, b::Int) = a + b
f(a::Float64, b::Float64) = a * b
f
end
where f
would be replaced by the macroexpander hygienically.
It’s just that there is little point in doing so.
Can’t you do
foo = x -> …
(::typeof(foo))(x, y) = …
?