circular-enum
Copyright(c) 2023-2026 Mirko Westermeier
LicenseMIT
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Enum.Circular

Description

Sometimes, bounded enum types should be circular. Consider this enum type of directions:

data Direction  = North
                | East
                | South
                | West
                deriving (Eq, Enum, Bounded)

The Enum instance allows for succ North to be East and succ East to be South. But in this case, one would like to have some kind of succ with succ West = North again. With Eq and Bounded instances, the functions defined in this module act like circular versions of succ and pred.

Note: this module is designed for small, finite enum types created with deriving Enum. It is not suitable for types where fromEnum maxBound + 1 overflows Int, such as Int or Word itself.

Synopsis

Documentation

csucc :: (Eq a, Enum a, Bounded a) => a -> a Source #

Circular version of succ

cpred :: (Eq a, Enum a, Bounded a) => a -> a Source #

Circular version of pred

newtype Circular a Source #

Constructors

Circular 

Fields

Instances

Instances details
(Enum a, Bounded a) => Enum (Circular a) Source # 
Instance details

Defined in Data.Enum.Circular

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

Defined in Data.Enum.Circular

Methods

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

show :: Circular a -> String #

showList :: [Circular a] -> ShowS #

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

Defined in Data.Enum.Circular

Methods

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

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