# How to broadcast a value over functions instead of a function over values?

**URL:** https://discourse.julialang.org/t/how-to-broadcast-a-value-over-functions-instead-of-a-function-over-values/83877
**Category:** General Usage
**Tags:** question, broadcast
**Created:** [July 7, 2022, 5:55am UTC](https://discourse.julialang.org/t/how-to-broadcast-a-value-over-functions-instead-of-a-function-over-values/83877 "2022-07-07T05:55:00Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [July 7, 2022, 5:55am UTC](https://discourse.julialang.org/t/how-to-broadcast-a-value-over-functions-instead-of-a-function-over-values/83877/1 "2022-07-07T05:55:00Z")

</div>

This is a somewhat unusual usage, but I’m wondering if given

```julia
julia> M = [sin cos; cos sin]
2×2 Matrix{Function}:
 sin cos
 cos sin

```

is there an easy way to broadcast the function evaluation over the elements for one specific argument? I want the result

```julia
julia> [x(pi/3) for x in M]
2×2 Matrix{Float64}:
 0.866025 0.5
 0.5 0.866025

```

except using the broadcasting syntax instead of a comprehension. I thought that it’ll be something like

```julia
julia> getindex.(Ref(M), CartesianIndices(M)).(pi/3)
ERROR: MethodError: objects of type Matrix{Function} are not callable
Use square brackets [] for indexing an Array.

```

but this doesn’t work.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [July 7, 2022, 6:08am UTC](https://discourse.julialang.org/t/how-to-broadcast-a-value-over-functions-instead-of-a-function-over-values/83877/2 "2022-07-07T06:08:17Z")

</div>

Why not treat both `M` and `pi/3` like arguments for a separate method to broadcast?

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

julia> apply.([sin cos; cos sin], pi/3)
2×2 Matrix{Float64}:
 0.866025 0.5
 0.5 0.866025

```

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [July 7, 2022, 7:02am UTC](https://discourse.julialang.org/t/how-to-broadcast-a-value-over-functions-instead-of-a-function-over-values/83877/3 "2022-07-07T07:02:49Z")

</div>

Marking this as the solution, as the following works and is quite concise:

```julia
julia> M = [sin cos; cos sin]
2×2 Matrix{Function}:
 sin cos
 cos sin

julia> map.(M, pi/3)
2×2 Matrix{Float64}:
 0.866025 0.5
 0.5 0.866025

```

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [July 7, 2022, 7:06am UTC](https://discourse.julialang.org/t/how-to-broadcast-a-value-over-functions-instead-of-a-function-over-values/83877/4 "2022-07-07T07:06:02Z")

</div>

Oh I never realized `map` returns a `Number` if given a `Number`, good to know! Bear in mind this doesn’t work for scalars in general, `map` falls back to expecting an iterable:

```julia
julia> map(sin, pi/3)
0.8660254037844386

julia> struct A end

julia> f(::A) = 1
f (generic function with 1 method)

julia> map(f, A())
ERROR: MethodError: no method matching length(::A)

```

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [July 7, 2022, 7:34am UTC](https://discourse.julialang.org/t/how-to-broadcast-a-value-over-functions-instead-of-a-function-over-values/83877/5 "2022-07-07T07:34:25Z")

</div>

Presumably, iteration needs to be defined for the type for `map` to work correctly. The `apply` solution works as expected:

```julia
julia> M = [f f; f f];

julia> apply.(M, Ref(A()))
2×2 Matrix{Int64}:
 1 1
 1 1

```

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [July 7, 2022, 7:46am UTC](https://discourse.julialang.org/t/how-to-broadcast-a-value-over-functions-instead-of-a-function-over-values/83877/6 "2022-07-07T07:46:10Z")

</div>

There is actually a `map` method specifically for `Number`s to get around iteration so it doesn’t need to allocate an output vector.

```julia
julia> struct Num2{T<:Number} t::T end # iterable contains 1 Number
julia> Base.iterate(x::Num2) = iterate(x.t)
julia> Base.iterate(x::Num2, y) = iterate(x.t, y)
julia> Base.length(x::Num2) = length(x.t)

julia> map(sin, Num2(pi/3))
1-element Vector{Float64}:
 0.8660254037844386

julia> @which map(sin, Num2(pi/3))
map(f, A) in Base at abstractarray.jl:2896

julia> map(sin, pi/3)
0.8660254037844386

julia> @which map(sin, pi/3)
map(f, x::Number, ys::Number...) in Base at number.jl:272

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [July 7, 2022, 8:35am UTC](https://discourse.julialang.org/t/how-to-broadcast-a-value-over-functions-instead-of-a-function-over-values/83877/7 "2022-07-07T08:35:11Z")

</div>

```julia
julia> π/3 .|> M
2×2 Matrix{Float64}:
 0.866025 0.5
 0.5 0.866025

```
