range-1.0.0.0: An efficient and versatile range library.
Safe HaskellSafe
LanguageHaskell2010

Data.Range.Ord

Description

Ordering newtypes for Range.

Range deliberately has no Ord instance because there is no single natural ordering — the right choice depends on the use case. This module provides two explicit wrappers:

  • KeyRange — a consistent structural ordering, suitable for use as a Map key or in a Set.
  • SortedRange — a positional ordering by location on the number line, suitable for sorting ranges for display.

Example: Map keyed on ranges

import Data.Range (Range, (+=+), lbi)
import Data.Range.Ord (KeyRange(..))
import qualified Data.Map.Strict as Map

type RuleMap = Map (KeyRange Integer) String

rules :: RuleMap
rules = Map.fromList
  [ (KeyRange (1 +=+ 10),  "low")
  , (KeyRange (11 +=+ 50), "medium")
  , (KeyRange (lbi 51),    "high")
  ]

Example: sorting ranges by position on the number line

import Data.List (sortOn)
import Data.Range (Range, (+=+), lbi, ube)
import Data.Range.Ord (SortedRange(..))

sortOn SortedRange [lbi 10, 1 +=+ 5, ube 0 :: Range Integer]
-- [ube 0, 1 +=+ 5, lbi 10]

-- or equivalently:
displayRanges :: Ord a => [Range a] -> [Range a]
displayRanges = sortOn SortedRange
Synopsis

Structural ordering

Use KeyRange when you need Range values as Map keys or in a Set. The ordering is consistent but not semantically meaningful on the number line.

newtype KeyRange a Source #

Wraps Range with a structural Ord instance, suitable for use as a Map key or in a Set.

Constructor order: SingletonRange < SpanRange < LowerBoundRange < UpperBoundRange < InfiniteRange. Fields within the same constructor are compared lexicographically.

This ordering is not semantically meaningful on the number line — SingletonRange 5 and SpanRange (Bound 5 Inclusive) (Bound 5 Inclusive) are considered distinct. It is only appropriate where any consistent total order will do (deduplication, Map keys).

Use unKeyRange to unwrap the underlying Range.

See also SortedRange for ordering by position on the number line.

Since: 0.3.2.0

Constructors

KeyRange 

Fields

Instances

Instances details
Show a => Show (KeyRange a) Source # 
Instance details

Defined in Data.Range.Ord

Methods

showsPrec :: Int -> KeyRange a -> ShowS

show :: KeyRange a -> String

showList :: [KeyRange a] -> ShowS

Eq a => Eq (KeyRange a) Source # 
Instance details

Defined in Data.Range.Ord

Methods

(==) :: KeyRange a -> KeyRange a -> Bool

(/=) :: KeyRange a -> KeyRange a -> Bool

Ord a => Ord (KeyRange a) Source # 
Instance details

Defined in Data.Range.Ord

Methods

compare :: KeyRange a -> KeyRange a -> Ordering

(<) :: KeyRange a -> KeyRange a -> Bool

(<=) :: KeyRange a -> KeyRange a -> Bool

(>) :: KeyRange a -> KeyRange a -> Bool

(>=) :: KeyRange a -> KeyRange a -> Bool

max :: KeyRange a -> KeyRange a -> KeyRange a

min :: KeyRange a -> KeyRange a -> KeyRange a

Positional ordering

Use SortedRange when you want to sort ranges by where they sit on the number line (lower bound first, upper bound as tiebreaker).

newtype SortedRange a Source #

Wraps Range with a positional Ord instance: ranges are ordered by where they sit on the number line, lower bound first with upper bound as a tiebreaker.

The Eq instance is consistent with Ord: two SortedRange values are equal iff they have the same lower and upper bounds. This means SortedRange (SingletonRange 5) and SortedRange (5 +=+ 5) are considered equal (they occupy the same point on the number line).

Use unSortedRange to unwrap the underlying Range. Typical usage:

>>> import Data.List (sortOn)
>>> sortOn SortedRange [SingletonRange 5, SingletonRange 1, SingletonRange 3 :: Range Integer]
[SingletonRange 1,SingletonRange 3,SingletonRange 5]

See also KeyRange for a structural ordering suitable for Map keys.

Since: 0.3.2.0

Constructors

SortedRange 

Fields

Instances

Instances details
Show a => Show (SortedRange a) Source # 
Instance details

Defined in Data.Range.Ord

Methods

showsPrec :: Int -> SortedRange a -> ShowS

show :: SortedRange a -> String

showList :: [SortedRange a] -> ShowS

Ord a => Eq (SortedRange a) Source # 
Instance details

Defined in Data.Range.Ord

Methods

(==) :: SortedRange a -> SortedRange a -> Bool

(/=) :: SortedRange a -> SortedRange a -> Bool

Ord a => Ord (SortedRange a) Source # 
Instance details

Defined in Data.Range.Ord

Methods

compare :: SortedRange a -> SortedRange a -> Ordering

(<) :: SortedRange a -> SortedRange a -> Bool

(<=) :: SortedRange a -> SortedRange a -> Bool

(>) :: SortedRange a -> SortedRange a -> Bool

(>=) :: SortedRange a -> SortedRange a -> Bool

max :: SortedRange a -> SortedRange a -> SortedRange a

min :: SortedRange a -> SortedRange a -> SortedRange a