# 0.6: how do you broadcast \`call\` over a vector of functions?

**URL:** <https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772>\
**Category:** General Usage\
**Tags:** broadcast\
**Created:** [March 20, 2017, 9:11am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772 "2017-03-20T09:11:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 20, 2017, 9:11am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/1 "2017-03-20T09:11:32Z")

</div>

I’m trying to broadcast a function call over a vector of functions, but `call.(f,0.1)` is deprecated. I tried `f.(0.1)` but it doesn’t work:

```julia
julia> f=[x->x,x->x^2]
2-element Array{Function,1}:
 #5
 #6

julia> f.(0.1)
ERROR: MethodError: objects of type Array{Function,1} are not callable
Use square brackets [] for indexing an Array.
 in broadcast(::Array{Function,1}, ::Float64) at ./broadcast.jl:16

julia> call.(f,0.1)
WARNING: call(f,args...) is deprecated, use f(args...) instead.
 in depwarn(::String, ::Symbol) at ./deprecated.jl:64
 in ##1003#1004(::Array{Any,1}, ::Function, ::Function, ::Float64, ::Vararg{Float64,N}) at ./deprecated.jl:272
 in (::Base.##1003#1005)(::Function, ::Float64, ::Vararg{Float64,N}) at ./deprecated.jl:271
 in broadcast_t(::Function, ::Type{Any}, ::Array{Function,1}, ::Vararg{Any,N}) at ./broadcast.jl:222
 in broadcast(::Function, ::Array{Function,1}, ::Float64) at ./broadcast.jl:230
 in eval(::Module, ::Any) at ./boot.jl:234
 in eval(::Module, ::Any) at /Users/solver/Projects/julia/usr/lib/julia/sys.dylib:?
 in eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:64
 in macro expansion at ./REPL.jl:95 [inlined]
 in (::Base.REPL.##3#4{Base.REPL.REPLBackend})() at ./event.jl:68
while loading no file, in expression starting on line 0
WARNING: call(f,args...) is deprecated, use f(args...) instead.
 in depwarn(::String, ::Symbol) at ./deprecated.jl:64
 in ##1003#1004(::Array{Any,1}, ::Function, ::Function, ::Float64, ::Vararg{Float64,N}) at ./deprecated.jl:272
 in macro expansion at ./broadcast.jl:192 [inlined]
 in _broadcast!(::Base.##1003#1005, ::Array{Float64,1}, ::Tuple{Tuple{Bool},Tuple{}}, ::Tuple{Tuple{Int64},Tuple{}}, ::Tuple{Array{Function,1},Float64}, ::Type{Val{2}}, ::CartesianRange{CartesianIndex{1}}, ::CartesianIndex{1}, ::Int64) at ./broadcast.jl:179
 in broadcast_t(::Function, ::Type{Any}, ::Array{Function,1}, ::Vararg{Any,N}) at ./broadcast.jl:225
 in broadcast(::Function, ::Array{Function,1}, ::Float64) at ./broadcast.jl:230
 in eval(::Module, ::Any) at ./boot.jl:234
 in eval(::Module, ::Any) at /Users/solver/Projects/julia/usr/lib/julia/sys.dylib:?
 in eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:64
 in macro expansion at ./REPL.jl:95 [inlined]
 in (::Base.REPL.##3#4{Base.REPL.REPLBackend})() at ./event.jl:68
while loading no file, in expression starting on line 0
2-element Array{Float64,1}:
 0.1 
 0.01

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 20, 2017, 9:24am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/2 "2017-03-20T09:24:22Z")

</div>

`(x->x(.1)).(f)` would do it, but probably not what you are looking for.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 20, 2017, 10:02am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/3 "2017-03-20T10:02:29Z")

</div>

That would recompile for every value you evaluate at

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 20, 2017, 10:08am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/4 "2017-03-20T10:08:05Z")

</div>

> [@dlfivefifty](#):
>
> That would recompile for every value you evaluate at

Would it?

```julia
fs=[x->x,x->x^2];
apply(f, xx) = (x->x(xx)).(f)

```

Now

```julia
<< @time apply(fs, 2.0) # First run, compilation overhead
  0.947333 seconds (1.37 M allocations: 63.188 MiB, 3.33% gc time)
<< @time apply(fs, 3.0) # Second at different value
  0.000115 seconds (53 allocations: 1.875 KiB)

```

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 20, 2017, 10:12am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/5 "2017-03-20T10:12:55Z")

</div>

Yes making your own `call` implementation will solve the problem, though it’s a bit annoying that you can’t access it directly any more.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 20, 2017, 10:13am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/6 "2017-03-20T10:13:47Z")

</div>

What do you mean with “access it directly”?

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 20, 2017, 10:17am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/7 "2017-03-20T10:17:35Z")

</div>

There used to be a function `call` that corresponded to calling a function, which is now deprecated. So I mean accessing `call` without having a deprecation warning.

But this is a very minor issue (in fact, I already had such a function but I was hoping for something more canonical)

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [March 20, 2017, 10:24am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/8 "2017-03-20T10:24:53Z")

</div>

If speed is a concern and you have a small set of functions, then `f` being a tuple of functions could be much better. `map` should work, with a closure.

Otherwise, you would end up with, at the least, a dynamic dispatch each call (two, if you include an anonymous function/closure to wrap it).

For long lists of functions where you need speed too, you’ll need to arrange your own dispatch manually. Vectors populated by `cfunction` followed by `ccall` could be one option, or if the functions are a fixed, known set, you could keep an `Int` and branch to the correct call. These options work best if the return types are all the same - if not you pretty much need to use a tuple of functions.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 20, 2017, 10:28am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/9 "2017-03-20T10:28:04Z")

</div>

Speed isn’t that much of an issue since the function calls themselves are fairly expensive, so a dynamic dispatch is fine. Compiling takes forever, however.

It’s also ugly.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 20, 2017, 10:48am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/10 "2017-03-20T10:48:44Z")

</div>

Also:

```julia
julia> f=[x->x,x->x^2]
2-element Array{Function,1}:
 #1
 #2

julia> @time (x->x(.1)).(f)
  1.129888 seconds (1.69 M allocations: 68.463 MiB, 9.62% gc time)
2-element Array{Float64,1}:
 0.1 
 0.01

julia> @time (x->x(.1)).(f)
  0.042708 seconds (9.63 k allocations: 480.332 KiB)
2-element Array{Float64,1}:
 0.1 
 0.01

julia> @time (x->x(.2)).(f)
  0.029566 seconds (9.63 k allocations: 480.598 KiB)
2-element Array{Float64,1}:
 0.2 
 0.04

```

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 20, 2017, 10:53am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/11 "2017-03-20T10:53:27Z")

</div>

Those timings are pretty bad…

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [March 20, 2017, 11:06am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/12 "2017-03-20T11:06:31Z")

</div>

As an illustration:

```julia
julia> f=(x->x,x->x^2)
(#5,#6)

julia> apply(fs::Tuple, x) = _apply(x, (), fs...)
apply (generic function with 1 method)

julia> _apply(x, out::Tuple) = out
_apply (generic function with 1 method)

julia> @inline _apply(x, out::Tuple, f, fs...) = _apply(x, (out..., f(x)), fs...)
_apply (generic function with 2 methods)

julia> @time apply(f, 0.1)
  0.005076 seconds (2.81 k allocations: 133.484 KB)
(0.1,0.010000000000000002)

julia> @time apply(f, 0.1)
  0.000002 seconds (5 allocations: 192 bytes)
(0.1,0.010000000000000002)

julia> @time apply(f, 0.2)
  0.000002 seconds (5 allocations: 192 bytes)
(0.2,0.04000000000000001)

```

That’s a 10^4 speed improvement.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 20, 2017, 11:20am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/13 "2017-03-20T11:20:27Z")

</div>

Ah OK, in my actual use case they are `Fun`s, not anonymous functions, so the overhead in compiling the entries is not there, but the overhead in compiling `x->x(0.1)` is

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [March 20, 2017, 11:26am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/14 "2017-03-20T11:26:05Z")

</div>

What’s a `Fun`?

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 20, 2017, 11:43am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/15 "2017-03-20T11:43:32Z")

</div>

From ApproxFun, represents a function as coefficients in a basis (so no need to compile for different functions)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 20, 2017, 11:54am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/16 "2017-03-20T11:54:24Z")

</div>

Plz stop timing things in global scope…

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 20, 2017, 11:57am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/17 "2017-03-20T11:57:44Z")

</div>

😊 sorry . In my case, I didn’t mean for the actual timings to mean anything, just to say I had the impression it wasn’t recompiling for new values of the floating point argument. Point taken, of course.

---

<div class="post-metadata">

**Author:** ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)\
**Post date:** [March 20, 2017, 5:06pm UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/18 "2017-03-20T17:06:39Z")

</div>

Couldn’t you define `f = x -> [x, x^2]`?

`f.(0.1)` would work with the anonymous function above.

EDIT: An example of the above for `Fun` (pun intended)

```julia
g = Fun(identitiy, -1..1)
h = Fun(x->x^2, -1..1)
f = x -> [g(x), h(x)]
f.(0.1) == [g(0.1), h(0.1)] #true

```

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 20, 2017, 6:27pm UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/19 "2017-03-20T18:27:17Z")

</div>

No: its in a function that is working with a long `Vector{Fun}`. Fortunately for `Fun`s there is already a function `evaluate(f,x)` that works fine.

The question was more curiousness than looking for work arounds. I think the answer is “No, you cannot directly broadcast a function call”

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 21, 2017, 6:54am UTC](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772/20 "2017-03-21T06:54:13Z")

</div>

This is mostly unreadable but I think it can qualify as directly.

```julia
julia> fs = [x->x, x->x^2]
2-element Array{Function,1}:
 #1
 #2

julia> (|>).(2.0, fs)
2-element Array{Float64,1}:
 2.0
 4.0

```

Personally I would just define my own call function (call deprecation is gone from 0.6) and use that.

```julia
julia> call(f, x...) = f(x...)
call (generic function with 1 method)

julia> call.(fs, 2.0)
2-element Array{Float64,1}:
 2.0
 4.0

```

[Next page](https://discourse.julialang.org/t/0-6-how-do-you-broadcast-call-over-a-vector-of-functions/2772.md?page=2)
