a name indexed by a number containing a function which references yet
another value, you say. these things sound familiar….

in fact, these things are first class citizens in julia:
“a name indexed by a number” — an Array
“a function” — a Function
“which references yet another value” — a Closure

And they are very easy to use:

# create the list of functions
errors = [-1,2,445,5,2,-2,-223,2,6,7]
myfunctions = [r -> err*r + 7 for err in errors]

# call the first one
f1 = myfunctions[1]
f1(r)

In addition to being more readable than scattering $’s and eval’s
everywhere, this is more memory efficient, faster, and more flexible.
Pretty much a win all around :)
​

On Sun, Oct 5, 2014 at 2:57 PM, Yakir Gagnon <[email protected]> wrote:

> Awesome!!!
> That's exactly what I needed! I missed the need for symbol in there, and
> tried to put the $k part together with everything else. This is great,
> thanks Toivo!
>
>
> On Monday, October 6, 2014 4:50:14 AM UTC+10, Toivo Henningsson wrote:
>>
>> I think that you will have to provide a little more detail. When you eval
>> code, the name of a function will be specified by a symbol, which you can
>> create using symbol(name), where name is a string. Then you have to
>> interpolate the function name, eg
>>
>> @eval $(symbol("f$k"))(x) = x + $k
>>
>> should create a function depending on the value in the variable k, eg
>> when k = 10,
>>
>> f10(x) = x + 10
>>
>>

Reply via email to