Skip to content

Representation and basic operations for ranges of types isomorphic to integers.

License

Notifications You must be signed in to change notification settings

tpapp/DiscreteRanges.jl

Repository files navigation

DiscreteRanges

Lifecycle Build Status Coverage Status codecov.io

Introduction

This package provides a type DiscreteRange{T} for types T which are isomorphic to integers, in the sense that elements of T can be thought of as evenly spaced values using some relevant arithmetic operations. All subtypes of Integer satisfy this, and also Date.

DiscreteRange(left::T, right::T) is meant to represent all values x::T such that a ≤ x ≤ b. In this sense it is very similar to a UnitRange, except that it can be defined for types other than <: Real, and extended easily (see below). Iteration, indexing and array interfaces are supported, and so are basic set operations (, , , , etc).

Comparison and conflict with IntervalSets.jl

IntervalSets.jl is a similar library serving a different purpose. Both export .., you can avoid conflicts by importing only DiscreteRange and optionally using another infix operator:

using DiscreteRanges: DiscreteRange
const  = DiscreteRanges.:(..) # or pick your favorite operator
12                            # DiscreteRange{Int}(1, 2)

Then comparing the two packages, keep in mind that IntervalSets.jl is better for representing intervals of real numbers, while a DiscreteRange is a collection of a finite number of objects.

The following table summarizes the differences:

DiscreteRange IntervalSets.ClosedInterval
3 ∈ 1..3 true true
3.0 ∈ 1..3 true (converted first) true (compared as is)
3.1 ∈ 1..3 throws InexactError true
1.0..3.0 throws ArgumentError (non-discrete type) valid
(1..2) ∪ (3..4) 1..4 throws ArgumentError (disjoint)
length(1..3) 3 3 (this may change)
width(1..3) throws MethorError 2
(1..2) ∈ (0..10) throws MethorError true

Extending for custom types

To make DiscreteRange work for a type T, define isdiscrete, discrete_gap, and discrete_next. See their docstring for further information. There is an example in the unit tests.

About

Representation and basic operations for ranges of types isomorphic to integers.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages