There are a number of issues about type inference for `map` and similar
functions. See 6692 and the issues linked therein:
https://github.com/JuliaLang/julia/pull/6692


On Fri, May 30, 2014 at 4:27 AM, Tomas Lycken <[email protected]>
wrote:

> I have a `DataArray{Float64,1}` named `ps` with a bunch of values I'd like
> to map to their bin indices in a histogram, and a function `histidx` that
> maps a single value (based on endpoints of the histogram and number of
> bins). I try the following:
>
> idxs = map(p -> histidx(minx, maxx, Nbins, p), ps)
>
> and the result is a `DataArray{Any,1}`. However, I know that `histidx` can
> only return integers:
>
> function histidx(xmin, xmax, Nbins::Integer, x)
>     if x <= xmin
>         return 1
>     elseif x >= xmax
>         return Nbins
>     else
>         return iceil((x-xmin)/((xmax-xmin)/Nbins))
>     end
> end
>
> Why isn't map able to infer that the type of the resulting array could be
> tighter?
>
> I try to help it in various ways, but without success:
>
> julia> typeof(map(p -> histidx(minx, maxx, Nbins, p)::Integer, ps))
> DataArray{Any,1}
> julia> foo() = map(p -> histidx(minx, maxx, Nbins, p); typeof(foo())
> DataArray{Any,1}
>
> The only thing that seems to work is manually doing this on the underlying
> data instead:
>
> julia> typeof(map(p -> histidx(minx, maxx, Nbins, p), ps.data))
> Array{Int64,1}
>
> This is troublesome, since it a) requires me to handle `Array`s and
> `DataArray`s differently when I do this mapping, and b) doesn't allow for
> missing data in the `DataArray`s.
>
> I think the correct behavior should be to return a DataArray{Int64,1},
> with each `NA` value mapped to `NA` in the output.
>
> // T
>

Reply via email to