map([A, B, C]) do x
if x < 0 && iseven(x)
return 0
elseif x == 0
return 1
else
return x
end
end
map((x)->..., [A, B, C])
begin
if x < 0 && iseven(x)
return 0
elseif x == 0
return 1
else
return x
end
end
somefunc(A, B, (x,y)->...)
begin
return y, x
end
I like the alternative syntax! +1 =)
You can just as easily define f separately and pass it to the function; no ‘where’ clause needed.
Yes, of course. That is still an option for map
, open
et al, but the new syntax still has the advantage of making it very clear exactly where the function is going to be used. I find that when I use anonymous functions in various contexts, it’s usually because I know I won’t be re-using the function anywhere else, and therefore I don’t want to define anything that clutters up the name space. The proposed syntax (both the `(x,y)->...` variant and the `where` clause) still solve that problem, just as well as the current syntax with do
blocks.
// T
v |> f |> 1+_ |> sort(_, rev=true)
That is a cool suggestion. Would you also be able to do something like this
remotecall(2, _, args...) do x
# blah
end
If so, it would accomplish what the where
syntax was trying to do.
Since no one else has, I'll chime in in support of the current do block. If anything the proposal seems less intuitive/elegant to me.The where block is nicer but a new keyword/syntax is going to need a pretty compelling reason to exist, and in this case you can simply write:function f(x)# code...endmap(f, 1:10)Within a lexical scope (let block, function body, whatever) this will create an anonymous function rather than cluttering the global namespace. It also has the advantage that you can use multiple dispatch:function test(xs)f(x::Int) = :intf(x::Real) = :realmap(f, xs)endThis is more flexible and IMO clearer, so long story short I think a major change in this area will be unlikely.
let
local f
function f(x::Int)
return x*x
end
map(f, [1 2 3])
end
To me the major driver for some variation on the current syntax is wanting to chain calls via the |> operator or something like it. I.e. something like this:v |> f |> 1+_ |> sort(_, rev=true)This is somewhat related to the idea of having an even terser anonymous function syntax.
v |> f(...) |> 1+... |> sort(..., rev=true)