StaticRanges.jl - Static and mutable range types

Just released the second minor version of my first package (was still trying to figure out all the registering stuff in v0.1.0).

It is meant to mirror the behavior of base as closely as possible. So if you know how to use range in base you can get mutable ranges with mrange and static ranges with srange using the same syntax. The one difference is that you can call mrange(1, 10) without any extra keyword arguments. This is primarily because I can’t overload the colon and got tired of typing mrange(1, stop=10).

Features

  • More comprehensive coverage of ranges than StaticArrays. There are parallel types for:
    • OneTo
    • UnitRange
    • StepRange
    • LinRange
    • StepRangeLen
  • Easier syntax for static ranges than is available in StaticArrays. You can just use srange instead of SUnitRange.
  • Includes mutable counterparts as well as static.

Remaining Work

  • Testing: At this point the majority of future work will focus on testing and documenting examples. I have most of the tests from base Julia reimplemented for this package. Also note that test coverage should be at least at 70% but an entire file that covers promote_rule is inlined away. However, a lot of operators (*, +, -, etc) need to be more robustly tested.
  • Performance: Although the user facing API should be fairly stable at this point there will likely be some internal changes as I continue to improve performance. I will update this post with performance measures as soon as they are in a more consumable form.
  • Documentation: Every new method and type should have a docstring but most don’t have examples yet. I’ll probably focus on this after I get more test coverage as most behavior should be pretty intuitive if you have used ranges in Julia before.

Feel free to file issues as you find them and of course pull requests solving issues are welcome.

Note that you can also do range(1, 10), I think since 1.1.

Can you please demonstrate a use case for this package?

Also, are you aware of StaticArrays.SUnitRange?

On 1.3 you need a keyword argument also (at least for my version).

I’ve updated the original post but will have better examples as I plot out the benchmarks. I created this package primarily because I need it for other things so I will also link to those as soon as they are ready.

Sorry, I was not clear: of course you need either step or length, but stop does not have to be a keyword since 1.1.

Sorry to be obtuse, but what’s static about eg UnitMRange?

Your code has

mutable struct UnitMRange{T<:Real} <: StaticUnitRange{T}
    start::T
    stop::T

    function UnitMRange{T}(start, stop) where {T<:Real}
        return new(start, Base.unitrange_last(start,stop))
    end
end

and the length does not seem to be encoded in the type. It just looks like a mutable Base.UnitRange, which I am not entirely sure is a good idea.

1 Like

Yeah, that’s a terrible name for the supertype, but the most appropriate name was AbstractUnitRange. I just stuck with the convention from StaticArrays where everything is a subtype of StaticArray even if there are non static dimensions.

It probably isn’t very helpful most of the time, but I have some very specific use cases where I wanted to use it. For example, I have some non mutable structs that also have a range that needs to change sizes, but everything else is the same.