The problem is not with the return value of the passed functions, it is 
with inferring the type of `f`. Each function has it's own type and that in 
turn makes `f` bind to different types (first f1 then f2) in the loop i.e a 
type instability.


On Monday, August 1, 2016 at 5:53:23 AM UTC+2, Christoph Ortner wrote:
>
> Consider the following code snippet which shows the following expect 
> problem (on v0.5): if I form a tuple of functions, pass this tuple to 
> another function, then julia cannot infer enough information about them and 
> runs into a type instability. 
>
> MY QUESTION is: is there a work-around?  I.e., can I form an array, tuple, 
> etc of functions and call them without losing type stability?
>
> (declaring return-type did not help, which surprised me since I thought 
> that would give the additional information about what the two functions f1, 
> f2 in the tuple do)
>
>
> function test(N, ff)
>     r = 0.234; s = 0.0
>     for n = 1:N, f in ff
>         s = s + f(r)::Float64
>     end 
>     return s
> end 
>
>
> function test2(N, f1, f2)
>     r = 0.234; s = 0.0
>     for n = 1:N
>         s = s + f1(r) + f2(r)
>     end 
>     return s
> end 
>
>
> f1(r::Float64)::Float64 = r^3
> f2(r::Float64)::Float64 = r^5
>
>
> test(10, (f1,f2))
> test(10, (f1,f1))
> test2(10, f1,f2)
>
>
> @time test(1_000_000, (f1,f2))    # 0.079190 seconds (4.00 M 
> allocations...
> @time test2(1_000_000, f1, f2)    # 0.002279 seconds (5 allocations: 176 
> bytes)
> @time test(1_000_000, (f1,f1))    # 0.002664 seconds (5 allocations: 176 
> bytes)
>
>
>
>

Reply via email to