How to create a vector of functions and index in a for loop

Hi, I want to create a vector of functions in julia, so that I can index in a for loop:

my code is:

qxloc1 =   [qxloc(x) = 10
            qxloc(x) = 10]


qyloc2 = [qyloc(x) = 10
          qyloc(x) = 10]


for e = 1:2
    qxloc1[e]
             
end

I get the following error:

syntax: misplaced assignment statement in "[qxloc(x) = begin
# REPL[20], line 1

qxloc1 =   [ x -> 10
             x- > 10 ]

does the trick I guess…

2 Likes

@Sbeltranj, it looks odd as example…
Could you explain a bit further the objective?

An alternative to the anonymous functions in previous post:

qxloc(x) = 10
qxloc1 = [qxloc; qxloc]

qxloc1[1](5)
10
2 Likes
julia> f(x) = x; g(x) = 2x; h(x) = 3x
h (generic function with 1 method)

julia> x = 1
1

julia> for ϕ in (f, g, h)
          @show ϕ(x)
       end
ϕ(x) = 1
ϕ(x) = 2
ϕ(x) = 3

julia> ϕ = [f, g, h]
3-element Vector{Function}:
 f (generic function with 1 method)
 g (generic function with 1 method)
 h (generic function with 1 method)

julia> for i in 1:3
          @show ϕ[i](x)
       end
(ϕ[i])(x) = 1
(ϕ[i])(x) = 2
(ϕ[i])(x) = 3
4 Likes