If you declare `xs` as constant, the compiler knows that its type won't 
change, and can properly do inference.

julia> function runge(x::Number)
           1 / (1 + 5 * x^2)
       end
runge (generic function with 1 method)

julia> const xs = linspace(-1.0, 1.0);

julia> [runge(x) for x = xs]
100-element Array{Float64,1}:
 0.166667
 0.172413
 ⋮       
 0.17844 
 0.172413
 0.166667


It also works without const in a function, because again, the compiler can 
determine that the type of xs won't change:

julia> function testfunc()
          xs = linspace(-1.0, 1.0)
          [runge(x) for x = xs]
       end
testfunc (generic function with 1 method)

julia> testfunc()
100-element Array{Float64,1}:
 0.166667
 0.172413
 0.17844 
 ⋮      
 0.17844 
 0.172413
 0.166667

Cheers,
   Kevin


On Wednesday, May 7, 2014 7:10:20 AM UTC-7, John Myles White wrote:
>
> Comprehensions work much better if you type them:
>
> ys = Float64[runge(x) for x in xs]
>
>  — John
>
> On May 7, 2014, at 7:08 AM, Hans W Borchers <[email protected]<javascript:>> 
> wrote:
>
> Thanks a lot.
>
> Before putting this question I searched for "map" through the Julia 0.3 
> Manual (PDF version)
> and did not find a reference to a map() function. Now repeating this 
> procedure I see it. Sorry.
>
> I am still wondering what comprehension is good for if it dismisses all 
> the type information.
>
>
> On Wednesday, May 7, 2014 1:58:40 PM UTC+2, Tobias Knopp wrote:
>>
>> try 
>>     
>>     ys = map(runge,xs)
>>
>  
>
>
>

Reply via email to