According to the documentation, all this keyword does is create a lambda and insert it as the first argument of function call before it. This task could be accomplished with a macro with very similar syntax:
@do
map(coll) x -> x^2
I wanted to know the reason behind integrating this feature into the language as a keyword.
I donāt know if this is the reason, but I often use it for very complex lambdas. For example, the by() function for dataframes:
by(my_df, :col) do df
# a whole mess of stuff...
end
Iām not sure how easy your macro would be with a multiline expression, but it also doesnāt seem all that much simpler in the example you provided (in that case Iād probably just stick the lambda inside the function call).
You can use begin blocks for complex lambdas, theyāre apparently just syntactic sugar for anonymous functions. Hereās a macro which does exactly what do keyword does:
macro m_do(func, callable)
if !isa(func, Expr) || func.head != :call
error()
end
insert!(func.args, 2, callable)
return func
end
@m_do map([1,2,3]) x -> begin
println(x)
return x*x
end
It isnāt very complicated because the parser translates the map call and the lambda to two bulk sexprs (Iām not familiar with the source, but it certainly appears so).
This is an operation we want to be pretty because it is really commonly used.
It basically takes the place of pythonās with statements and more.
Basically this is a really common and useful pattern.
Some examples of the places I tend to use it:
open("file.txt") do fh
...
end
pmap(1:100) do x
...
end
mapreduce(hcat, 1:100) do x
# return a vector
end
lock(lk) do
...
end
withenv(API_KEY="123423") do
...
end
mktempdir() do dirname
...
end
sum(xs) do
...
end
I wouldnāt like to make all those uglier.
At the end of the day, why do we do anything?
Since julia is just going to compile to machine code anyway.
Or less reductio ad absurdum: why allow anon functions?
Why not just have people write functions normally?
Or use a macro to create real functions out of lambdas syntax?
Looking at the history do blocks have been in the language since before 0.1 release.
Which is before my time.
The original reason is thus probably buried in a email chain somewhere.
I donāt use do blocks, because I can never quite grok them, or at least only for a few hours at a time. I read the documentation, and then sort of get it, but next time around I have no idea.
I think it might be related to the spelling. āWho is doing what to whom?ā I can never read it correctly. Sometimes I try to replace it with Pythonās āwith ⦠as ā¦ā and then itās ok, but most of the time that doesnāt work. In summary, it might not be the concept thatās so hard, but the word do really throws me off.
Does anyone have a tip for how to make it sound right inside my head?
I am not sure I understand the requirement, but I would not invest too much in mapping it to a construct/sentence in a natural language, and would recommend trying to understand them as they are.
There is not much to do blocks, just syntactic sugar.
The reason I try to do that is that even when I occasionally figure out what it means, it never sticks, but seems to slip through my mental fingers, as it were. I donāt have this problem with other parts of the syntax. The substitution/reshuffling of the sequence of āthingsā, as well as the word do itself trips me up. I canāt help reading it as ādo x, yā (how can I do āx, yā?)
Anyway, thanks for the explanation. Maybe there is no ātrickā.