hgeometry-combinatorial-0.10.0.0: Data structures, and Data types.

Copyright(C) Frank Staals
Licensesee the LICENSE file
MaintainerFrank Staals
Safe HaskellNone
LanguageHaskell2010

Data.Range

Description

Data type for representing Generic Ranges (Intervals) and functions that work with them.

Synopsis

Documentation

data EndPoint a Source #

Endpoints of a range may either be open or closed.

Constructors

Open !a 
Closed !a 
Instances
Functor EndPoint Source # 
Instance details

Defined in Data.Range

Methods

fmap :: (a -> b) -> EndPoint a -> EndPoint b #

(<$) :: a -> EndPoint b -> EndPoint a #

Foldable EndPoint Source # 
Instance details

Defined in Data.Range

Methods

fold :: Monoid m => EndPoint m -> m #

foldMap :: Monoid m => (a -> m) -> EndPoint a -> m #

foldr :: (a -> b -> b) -> b -> EndPoint a -> b #

foldr' :: (a -> b -> b) -> b -> EndPoint a -> b #

foldl :: (b -> a -> b) -> b -> EndPoint a -> b #

foldl' :: (b -> a -> b) -> b -> EndPoint a -> b #

foldr1 :: (a -> a -> a) -> EndPoint a -> a #

foldl1 :: (a -> a -> a) -> EndPoint a -> a #

toList :: EndPoint a -> [a] #

null :: EndPoint a -> Bool #

length :: EndPoint a -> Int #

elem :: Eq a => a -> EndPoint a -> Bool #

maximum :: Ord a => EndPoint a -> a #

minimum :: Ord a => EndPoint a -> a #

sum :: Num a => EndPoint a -> a #

product :: Num a => EndPoint a -> a #

Traversable EndPoint Source # 
Instance details

Defined in Data.Range

Methods

traverse :: Applicative f => (a -> f b) -> EndPoint a -> f (EndPoint b) #

sequenceA :: Applicative f => EndPoint (f a) -> f (EndPoint a) #

mapM :: Monad m => (a -> m b) -> EndPoint a -> m (EndPoint b) #

sequence :: Monad m => EndPoint (m a) -> m (EndPoint a) #

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

Defined in Data.Range

Methods

(==) :: EndPoint a -> EndPoint a -> Bool #

(/=) :: EndPoint a -> EndPoint a -> Bool #

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

Defined in Data.Range

Methods

compare :: EndPoint a -> EndPoint a -> Ordering #

(<) :: EndPoint a -> EndPoint a -> Bool #

(<=) :: EndPoint a -> EndPoint a -> Bool #

(>) :: EndPoint a -> EndPoint a -> Bool #

(>=) :: EndPoint a -> EndPoint a -> Bool #

max :: EndPoint a -> EndPoint a -> EndPoint a #

min :: EndPoint a -> EndPoint a -> EndPoint a #

Read a => Read (EndPoint a) Source # 
Instance details

Defined in Data.Range

Show a => Show (EndPoint a) Source # 
Instance details

Defined in Data.Range

Methods

showsPrec :: Int -> EndPoint a -> ShowS #

show :: EndPoint a -> String #

showList :: [EndPoint a] -> ShowS #

Generic (EndPoint a) Source # 
Instance details

Defined in Data.Range

Associated Types

type Rep (EndPoint a) :: Type -> Type #

Methods

from :: EndPoint a -> Rep (EndPoint a) x #

to :: Rep (EndPoint a) x -> EndPoint a #

Arbitrary r => Arbitrary (EndPoint r) Source # 
Instance details

Defined in Data.Range

Methods

arbitrary :: Gen (EndPoint r) #

shrink :: EndPoint r -> [EndPoint r] #

NFData a => NFData (EndPoint a) Source # 
Instance details

Defined in Data.Range

Methods

rnf :: EndPoint a -> () #

type Rep (EndPoint a) Source # 
Instance details

Defined in Data.Range

type Rep (EndPoint a) = D1 (MetaData "EndPoint" "Data.Range" "hgeometry-combinatorial-0.10.0.0-KBhA9TX8xzkAsPm57THGXk" False) (C1 (MetaCons "Open" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 a)) :+: C1 (MetaCons "Closed" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 a)))

data Range a Source #

Data type for representing ranges.

Constructors

Range 

Fields

Instances
Functor Range Source # 
Instance details

Defined in Data.Range

Methods

fmap :: (a -> b) -> Range a -> Range b #

(<$) :: a -> Range b -> Range a #

Foldable Range Source # 
Instance details

Defined in Data.Range

Methods

fold :: Monoid m => Range m -> m #

foldMap :: Monoid m => (a -> m) -> Range a -> m #

foldr :: (a -> b -> b) -> b -> Range a -> b #

foldr' :: (a -> b -> b) -> b -> Range a -> b #

foldl :: (b -> a -> b) -> b -> Range a -> b #

foldl' :: (b -> a -> b) -> b -> Range a -> b #

foldr1 :: (a -> a -> a) -> Range a -> a #

foldl1 :: (a -> a -> a) -> Range a -> a #

toList :: Range a -> [a] #

null :: Range a -> Bool #

length :: Range a -> Int #

elem :: Eq a => a -> Range a -> Bool #

maximum :: Ord a => Range a -> a #

minimum :: Ord a => Range a -> a #

sum :: Num a => Range a -> a #

product :: Num a => Range a -> a #

Traversable Range Source # 
Instance details

Defined in Data.Range

Methods

traverse :: Applicative f => (a -> f b) -> Range a -> f (Range b) #

sequenceA :: Applicative f => Range (f a) -> f (Range a) #

mapM :: Monad m => (a -> m b) -> Range a -> m (Range b) #

sequence :: Monad m => Range (m a) -> m (Range a) #

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

Defined in Data.Range

Methods

(==) :: Range a -> Range a -> Bool #

(/=) :: Range a -> Range a -> Bool #

Show a => Show (Range a) Source # 
Instance details

Defined in Data.Range

Methods

showsPrec :: Int -> Range a -> ShowS #

show :: Range a -> String #

showList :: [Range a] -> ShowS #

Generic (Range a) Source # 
Instance details

Defined in Data.Range

Associated Types

type Rep (Range a) :: Type -> Type #

Methods

from :: Range a -> Rep (Range a) x #

to :: Rep (Range a) x -> Range a #

(Arbitrary r, Ord r) => Arbitrary (Range r) Source # 
Instance details

Defined in Data.Range

Methods

arbitrary :: Gen (Range r) #

shrink :: Range r -> [Range r] #

NFData a => NFData (Range a) Source # 
Instance details

Defined in Data.Range

Methods

rnf :: Range a -> () #

Ord a => IsIntersectableWith (Range a) (Range a) Source # 
Instance details

Defined in Data.Range

Methods

intersect :: Range a -> Range a -> Intersection (Range a) (Range a) Source #

intersects :: Range a -> Range a -> Bool Source #

nonEmptyIntersection :: proxy (Range a) -> proxy (Range a) -> Intersection (Range a) (Range a) -> Bool Source #

type Rep (Range a) Source # 
Instance details

Defined in Data.Range

type Rep (Range a) = D1 (MetaData "Range" "Data.Range" "hgeometry-combinatorial-0.10.0.0-KBhA9TX8xzkAsPm57THGXk" False) (C1 (MetaCons "Range" PrefixI True) (S1 (MetaSel (Just "_lower") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 (EndPoint a)) :*: S1 (MetaSel (Just "_upper") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 (EndPoint a))))
type IntersectionOf (Range a) (Range a) Source # 
Instance details

Defined in Data.Range

type IntersectionOf (Range a) (Range a) = NoIntersection ': (Range a ': ([] :: [Type]))

prettyShow :: Show a => Range a -> String Source #

Helper function to show a range in mathematical notation.

>>> prettyShow $ OpenRange 0 2
"(0,2)"
>>> prettyShow $ ClosedRange 0 2
"[0,2]"
>>> prettyShow $ Range (Open 0) (Closed 5)
"(0,5]"

lower :: forall a. Lens' (Range a) (EndPoint a) Source #

upper :: forall a. Lens' (Range a) (EndPoint a) Source #

pattern OpenRange :: a -> a -> Range a Source #

pattern ClosedRange :: a -> a -> Range a Source #

pattern Range' :: a -> a -> Range a Source #

A range from l to u, ignoring/forgetting the type of the endpoints

inRange :: Ord a => a -> Range a -> Bool Source #

Test if a value lies in a range.

>>> 1 `inRange` (OpenRange 0 2)
True
>>> 1 `inRange` (OpenRange 0 1)
False
>>> 1 `inRange` (ClosedRange 0 1)
True
>>> 1 `inRange` (ClosedRange 1 1)
True
>>> 10 `inRange` (OpenRange 1 10)
False
>>> 10 `inRange` (ClosedRange 0 1)
False

This one is kind of weird

>>> 0 `inRange` Range (Closed 0) (Open 0)
False

width :: Num r => Range r -> r Source #

Get the width of the interval

>>> width $ ClosedRange 1 10
9
>>> width $ OpenRange 5 10
5

clipLower :: Ord a => EndPoint a -> Range a -> Maybe (Range a) Source #

Clip the interval from below. I.e. intersect with the interval {l,infty), where { is either open, (, orr closed, [.

clipUpper :: Ord a => EndPoint a -> Range a -> Maybe (Range a) Source #

Clip the interval from above. I.e. intersect with (-infty, u}, where } is either open, ), or closed, ],

clampTo :: Ord r => Range r -> r -> r Source #

Clamps a value to a range. I.e. if the value lies outside the range we report the closest value "in the range". Note that if an endpoint of the range is open we report that value anyway, so we return a value that is truely inside the range only if that side of the range is closed.

>>> clampTo (ClosedRange 0 10) 20
10
>>> clampTo (ClosedRange 0 10) (-20)
0
>>> clampTo (ClosedRange 0 10) 5
5
>>> clampTo (OpenRange 0 10) 20
10
>>> clampTo (OpenRange 0 10) (-20)
0
>>> clampTo (OpenRange 0 10) 5
5

isValid :: Ord a => Range a -> Bool Source #

Check if the range is valid and nonEmpty, i.e. if the lower endpoint is indeed smaller than the right endpoint. Note that we treat empty open-ranges as invalid as well.

covers :: forall a. Ord a => Range a -> Range a -> Bool Source #

Wether or not the first range completely covers the second one

shiftLeft :: Num r => r -> Range r -> Range r Source #

Shift a range x units to the left

>>> prettyShow $ shiftLeft 10 (ClosedRange 10 20)
"[0,10]"
>>> prettyShow $ shiftLeft 10 (OpenRange 15 25)
"(5,15)"

shiftRight :: Num r => r -> Range r -> Range r Source #

Shifts the range to the right

>>> prettyShow $ shiftRight 10 (ClosedRange 10 20)
"[20,30]"
>>> prettyShow $ shiftRight 10 (OpenRange 15 25)
"(25,35)"