Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks to StepRange: Take 2 #6700

Merged
merged 2 commits into from
Jun 2, 2014
Merged

Tweaks to StepRange: Take 2 #6700

merged 2 commits into from
Jun 2, 2014

Conversation

quinnj
Copy link
Member

@quinnj quinnj commented Apr 30, 2014

The issorted and in changes are pretty straightforward, using the step for comparison. sort and sortperm remove the T<:Real restriction to apply to StepRange as well. All test/ranges.jl tests still pass with these changes, though I can add some more and try to break things if needed.

The introduction of steprem is annoying, but surprisingly minimal to allow full support for different step types than the difference between start and stop. The reasoning is that most Ordinal types shouldn't have to worry because the fallback to (stop-start) % step should work, but it allows for overloading (similar to how I'm overloading length).

Fixes #6638

cc: @JeffBezanson

Support methods I'm using to get range functionality for dates: https://github.com/karbarcca/Dates.jl/blob/master/src/ranges.jl
Relevant tests the date ranges are passing with these changes: https://github.com/karbarcca/Dates.jl/blob/master/test/test_ranges.jl

@quinnj
Copy link
Member Author

quinnj commented May 9, 2014

Bump. Thoughts on this? Too hackish? I'd really like to have this for 0.3 release.

@@ -54,6 +54,8 @@ immutable StepRange{T,S} <: OrdinalRange{T,S}
end
end

steprem(start,stop,step) = (stop-start) % step
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just override %?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind. I just looked a little closer at your implementation in Dates.

@JeffBezanson
Copy link
Member

I don't understand the implementation of steprem in Dates.jl. It seems to compute the last value, not a remainder. I might be missing something.

@quinnj
Copy link
Member Author

quinnj commented May 10, 2014

Drat, somehow my updated version got lost in some branch shuffling. I just pushed the version that works.

Base.steprem(a::Date,b::Date,c::Day) = (b-a) % c
Base.steprem(a::DateTime,b::DateTime,c::Millisecond) = (b-a) % c

function Base.steprem(start::TimeType,stop::TimeType,step::Period)
    start,stop = start > stop ? (stop,start) : (start,stop)
    step = step < zero(step) ? -step : step
    t = start
    while (t+step) <= stop
        t += step
    end
    return stop - t
end

@quinnj
Copy link
Member Author

quinnj commented May 10, 2014

I thought about having that manual definition of steprem in Base, and defining the version that uses % for all Reals, but then we'd be excluding other Ordinals that are non-Real, but implement % (somehow...); or at least making them define their own steprem. I don't know if there's a better way to work through all of this, but I keep coming back to the fact that this little patch allows for (seemingly) full functionality.

quinnj added a commit to quinnj/Dates.jl that referenced this pull request May 11, 2014
@quinnj
Copy link
Member Author

quinnj commented May 11, 2014

Ok, I've updated the Dates.jl range code and added a bunch more tests (based on this patch). Feel free to take a look and let me know if this is good to go or if we should try something else.

Dates.jl range code: https://github.com/karbarcca/Dates.jl/blob/master/src/ranges.jl
Dates.jl range tests: https://github.com/karbarcca/Dates.jl/blob/master/test/test_ranges.jl

@quinnj
Copy link
Member Author

quinnj commented May 28, 2014

Ok, added another round of testing/tweaks to the range code in Dates.jl (I updated all above links to relevant code and tests). I'm pretty satisfied with test coverage and that all the functionality is there. I think this should be merged. @JeffBezanson?

@quinnj
Copy link
Member Author

quinnj commented Jun 2, 2014

Ok, updated the issorted and in definitions.

JeffBezanson added a commit that referenced this pull request Jun 2, 2014
Tweaks to StepRange: Take 2
@JeffBezanson JeffBezanson merged commit 0c3de45 into JuliaLang:master Jun 2, 2014
@quinnj quinnj deleted the patch-3 branch June 2, 2014 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

StepRange with different step type than typeof(start - stop)
3 participants