# Mapping vector of functions to vector of numbers?

**URL:** <https://discourse.julialang.org/t/mapping-vector-of-functions-to-vector-of-numbers/20942>\
**Category:** New to Julia\
**Created:** [February 18, 2019, 4:10pm UTC](https://discourse.julialang.org/t/mapping-vector-of-functions-to-vector-of-numbers/20942 "2019-02-18T16:10:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [February 18, 2019, 4:10pm UTC](https://discourse.julialang.org/t/mapping-vector-of-functions-to-vector-of-numbers/20942/1 "2019-02-18T16:10:28Z")

</div>

Suppose I have a vector of functions, say `f=[sin,cos]` and I want to apply this element by element to a vector of numbers, say `v=[2pi/3, pi/3]` so that I get `[sin(2pi/3), cos(pi/3)]` – is there an elegant syntax for this?

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [February 18, 2019, 4:15pm UTC](https://discourse.julialang.org/t/mapping-vector-of-functions-to-vector-of-numbers/20942/2 "2019-02-18T16:15:26Z")

</div>

A comprehension is pretty elegant:

```julia
julia> f = [sin, cos]
2-element Array{Function,1}:
 sin
 cos

julia> v = [1, 2]
2-element Array{Int64,1}:
 1
 2

julia> [ff(vv) for (ff, vv) in zip(f, v)]
2-element Array{Float64,1}:
  0.8414709848078965
 -0.4161468365471424

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 18, 2019, 4:16pm UTC](https://discourse.julialang.org/t/mapping-vector-of-functions-to-vector-of-numbers/20942/3 "2019-02-18T16:16:32Z")

</div>

Wouldn’t you want to get out four elements like

```julia
f=[sin,cos] 
v=[2pi/3, pi/3]

julia> [i(j) for i in f for j in v]
4-element Array{Float64,1}:
  0.8660254037844387
  0.8660254037844386
 -0.4999999999999998
  0.5000000000000001

```

?

(Not clear from OP I guess)

---

<div class="post-metadata">

**Author:** ![under-Peter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/under-peter/32/3626_2.png) [@under-Peter](https://discourse.julialang.org/u/under-Peter)\
**Post date:** [February 18, 2019, 4:16pm UTC](https://discourse.julialang.org/t/mapping-vector-of-functions-to-vector-of-numbers/20942/4 "2019-02-18T16:16:44Z")

</div>

I would probably rely on an anonymous function that applies its first argument to its second argument, i.e.  
`(f,x) -> f(x)`.  
This you can then `map`

```julia
map((f,x) -> f(x), [sin, cos], [2\pi/3, \pi/3])

```

or `broadcast`

```julia
((f,x) -> f(x)).([sin, cos], [2\pi/3, \pi/3])

```

or you can rely on list comprehesion as the others suggest

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [February 18, 2019, 4:18pm UTC](https://discourse.julialang.org/t/mapping-vector-of-functions-to-vector-of-numbers/20942/5 "2019-02-18T16:18:01Z")

</div>

```julia
f = [sin, cos]
w = [π/2, π]

julia> map.(f, w)
2-element Array{Float64,1}:
  1.0
 -1.0

julia> w .|> f
2-element Array{Float64,1}:
  1.0
 -1.0

```

(by the way, if you want good performance, you might want to check out [FunctionWrappers.jl](https://github.com/yuyichao/FunctionWrappers.jl).)

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 18, 2019, 8:00pm UTC](https://discourse.julialang.org/t/mapping-vector-of-functions-to-vector-of-numbers/20942/6 "2019-02-18T20:00:48Z")

</div>

> [@under-Peter](#):
>
> I would probably rely on an anonymous function that applies its first argument to its second argument

The opposite might be easier, since it’s built into Julia:

```julia
f = [sin, cos]
w = [π/2, π]

julia> map(|>, w, f)
2-element Array{Float64,1}:
  1.0
 -1.0

julia> (|>).(w, f)
2-element Array{Float64,1}:
  1.0
 -1.0

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [February 19, 2019, 8:30pm UTC](https://discourse.julialang.org/t/mapping-vector-of-functions-to-vector-of-numbers/20942/7 "2019-02-19T20:30:51Z")

</div>

Thanks for excellent answers. I’ll take a look into the various options – have to finish some other things at work first :-o .
