It does indeed happens inside the function, if you pass a function as an 
argument to it (rather than refering to f implicitly in the function body, 
you explicitly pass in f as an extra argument)
see below:

julia> f(x) = x + 1
f (generic function with 1 method)

julia> g(f, xs) = [f(x) for x in xs]
g (generic function with 1 method)

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

julia> g(f,xs)
3-element Array{Any,1}:
 2
 3
 4


On Tuesday, November 4, 2014 2:22:24 AM UTC-5, Jutho wrote:
>
> This only happens in global scope, not inside a function? If you define
> f(list) = return [g(x) for x in list]
>
> then f(xs) will return an Array{Float64,1}. 
>
> Op dinsdag 4 november 2014 03:23:36 UTC+1 schreef K leo:
>>
>> I found that I often have to force this conversion, which is not too 
>> difficult.  The question why comprehension has to build with type Any? 
>>
>>
>> On 2014年11月04日 07:06, Miguel Bazdresch wrote: 
>> > > How could I force the type of gxs1 to be of an array of Float64? 
>> > 
>> > The simplest way is: 
>> > 
>> > gxs1 = Float64[g(x) for x in xs] 
>> > 
>> > -- mb 
>> > 
>> > On Mon, Nov 3, 2014 at 6:01 PM, Evan Pu <evanth...@gmail.com 
>> > <mailto:evanth...@gmail.com>> wrote: 
>> > 
>> >     Consider the following interaction: 
>> > 
>> >     julia> g(x) = 1 / (1 + x) 
>> >     g (generic function with 1 method) 
>> > 
>> >     julia> typeof(g(1.0)) 
>> >     Float64 
>> > 
>> >     julia> xs = [1.0, 2.0, 3.0, 4.0] 
>> >     4-element Array{Float64,1}: 
>> >      1.0 
>> >      2.0 
>> >      3.0 
>> >      4.0 
>> > 
>> >     julia> gxs1 = [g(x) for x in xs] 
>> >     4-element Array{Any,1}: 
>> >      0.5 
>> >      0.333333 
>> >      0.25 
>> >      0.2 
>> > 
>> >     Why isn't gxs1 type of Array{Float64,1}? 
>> >     How could I force the type of gxs1 to be of an array of Float64? 
>> > 
>> >     julia> gxs2 = [convert(Float64,g(x)) for x in xs] 
>> >     4-element Array{Any,1}: 
>> >      0.5 
>> >      0.333333 
>> >      0.25 
>> >      0.2 
>> > 
>> >     somehow this doesn't seem to work... 
>> > 
>> > 
>> > 
>> > 
>>
>>

Reply via email to