In this link, https://stackoverflow.com/a/22873866/20438781
it seems that there was an apply
function transfer a tuple to a function. However, it disappears in current version, and Varargs Functions still exists. Certainly Varargs Functions could substitute part of abilities of apply
, but I don’t consider there are the same thing.
My questions are, what is the complete function version of f(...)
? Is there any function with the same usage as apply
? And how to transfer a vector to a function without unpacking? Is it necessary to transform vector to tuple and be apply by a function?
apply
was deprecated in Julia 0.4 in favor of the splat operator.
$ ~/julia0.4/bin/julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.4.7 (2016-09-18 16:17 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-pc-linux-gnu
julia> apply(+, (1, 2, 3))
WARNING: apply(f, x) is deprecated, use `f(x...)` instead
You could trivially write one by writing
apply(f, args) = f(args...)
So there isn’t apply
anymore, just splat operator without function form. And there are also many operators without function form.
Most operators in Julia are just functions. The only ones that aren’t are the ones that manipulate syntax or have some kind of control flow effect, meaning that they could not be functions.
Anything that can be iterated over can be splatted. This includes vectors. However, in many cases it is more efficient to use a function that expects a vector instead of splatting. For example sum(x)
might be slightly faster than +(x...)
when x
is a vector.
What is it that you want to use this for?
Can you perhaps show an example where you think apply
is more convenient than splat? Personally, I find splat far nicer, so I’m curious.
You are definitely correct and I also agree splat is convenient. Just I hope every operator with a specific function form.
Oh, if I had known that was your actual concern I would have pointed out that there’s a function in Base.splat
(soon to be exported as Splat
in v1.9) that does this as a higher order function.
julia> Base.splat(+)((1,2,3,4))
10
That is, the old apply
can be written as
apply(f, args) = Base.splat(f)(args)
To ease this, I’ve added a PR to Compat.jl to export Splat
: add `Splat` by MasonProtter · Pull Request #785 · JuliaLang/Compat.jl · GitHub