# Broadcast call of callables in array

**URL:** <https://discourse.julialang.org/t/broadcast-call-of-callables-in-array/42468>\
**Category:** General Usage\
**Created:** [July 3, 2020, 10:29am UTC](https://discourse.julialang.org/t/broadcast-call-of-callables-in-array/42468 "2020-07-03T10:29:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [July 3, 2020, 10:29am UTC](https://discourse.julialang.org/t/broadcast-call-of-callables-in-array/42468/1 "2020-07-03T10:29:42Z")

</div>

I have an Array of callable structs that return vectors. I would like to call each of these structs with the same argument such that I get a matrix with column i being the vector returned by the callable i.

```julia
struct Foo
end
function (f::Foo)(x) return rand(5) end
foos = [Foo() for i in 1:3]

```

```julia
julia> foos.(rand(50))
ERROR: MethodError: objects of type Array{Foo,1} are not callable
Use square brackets [] for indexing an Array.

```

Using array comprehension returns an array of arrays:

```julia
julia> [foo(rand(50)) for foo in foos] |> typeof
Array{Array{Float64,1},1}

```

The desired outcome would be an `Array{Float64,2}`. In practice foo returns a `CuArray` so I get an Array of CuArrays.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 3, 2020, 10:42am UTC](https://discourse.julialang.org/t/broadcast-call-of-callables-in-array/42468/2 "2020-07-03T10:42:06Z")

</div>

Happy birthday!

Broadcasting with `foos.(A)` doesn’t work since it means calling `foos` with each element of `A`, and `foos` is not callable. Related issue: [https://github.com/JuliaLang/julia/issues/22129](https://github.com/JuliaLang/julia/issues/22129)

A possibility is to concatenate the arrays returned by the generator, using `hcat`:

```julia
hcat((foo(rand(50)) for foo in foos)...)

```

---

<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:** [July 3, 2020, 11:08am UTC](https://discourse.julialang.org/t/broadcast-call-of-callables-in-array/42468/3 "2020-07-03T11:08:04Z")

</div>

You can trick the broadcast into doing what you want. Not exactly any clearer than a generator though.

```julia
reduce(hcat, (Ref(rand(50)) .|> foos))

```
