> I don't really understand why you can't just use the StepRange for 
everything

You could. However, you can be more efficient by having special types (e.g. 
with UnitRange you don't need to check the steps).

>  `findin(::UnitRange, ::UnitRange)` returns another UnitRange

This seems good.

> `findin(::StepRange, ::StepRange)` returns an array of the indices

You might be able to do some maths here to return a StepRange... at least 
for Integers:

function Base.findin{T <: Integer}(a::StepRange{T}, b::StepRange{T})
    start = ??
    step = b.step รท gcd(a.step, b.step)
    stop = max(a.stop, b.stop)
    start:step:stop
end

but I 'm not sure this is possible with floats...

Looking in the source mentioning UnitRange here's a more serious 
bug: 
https://github.com/JuliaLang/julia/blob/e16c6784e1ba6f24b6faf0d282c78d8c14a1fbb3/base/array.jl#L866

julia> findin([5.2, 3.3], 3:20)
2-element Array{Int64,1}:
 1
 2

julia> findin([5.2, 3.3], 3:1:20)
0-element Array{Int64,1}

!

On Wednesday, 21 October 2015 19:59:20 UTC-7, Rory Finnegan wrote:
>
> Hi folks,
>
> I just had a couple questions about ranges that maybe someone can answer.
>
>
> 1) Why are there so many Range types? There is a UnitRange (start, stop), 
>  a StepRange (start, step, stop), and a FloatRange(start, step, length, 
> divisor), but I don't really understand why you can't just use the 
> StepRange for everything. Maybe I'm just missing some obvious use cases, 
> but this seems to lead to inconsistent or confusing behaviour like the 1 in 
> my second question.
>
> 2) `findin` seems to have inconsistent behaviour for ranges depending on 
> types in the ranges. `findin(::UnitRange, ::UnitRange)` returns another 
> UnitRange, but `findin(::StepRange, ::StepRange)` returns an array of the 
> indices. Is there some reason that these shouldn't be consistent?
>
> Ex:
> ```
> julia> findin(1:10, 2:5)
> 2:5
>
> julia> findin(1:1:10, 2:2:5)
> 2-element Array{Int64,1}:
>  2
>  4
>
> julia> r1 = DateTime(now())-Dates.Day(60):DateTime(now())
> 2015-08-22T19:07:47:1 day:2015-10-21T19:07:47
>
> julia> r2 = r1[1:20]
> 2015-08-22T19:07:47:1 day:2015-09-10T19:07:47
>
> julia> findin(r1, r2)
> 20-element Array{Int64,1}:
>   1
>   2
>   3
>   4
>   5
>   6
>   7
>   8
>   9
>  10
>  11
>  12
>  13
>  14
>  15
>  16
>  17
>  18
>  19
>  20
> ```
>

Reply via email to