What does this 'do' syntax mean?

Here is a trivial example:

my_function(f, container) = begin
    for element in container
        f(element)
    end
    return nothing
end

my_function([1,2,3]) do x  # equivlent to my_function(print, [1,2,3])
    print(x)
end

Instead of specifying (x)->print(x) as an argument of my_function, with the do synatx one can write (generally larger) functions that operate on the rest of the arguments in the signature (in this case, the argument container)

10 Likes