Module

# Data.Enum

Package
purescript-enums
Repository
purescript/purescript-enums

### #CardinalitySource

``newtype Cardinality a``

#### Constructors

• `Cardinality Int`

#### Instances

• `Newtype (Cardinality a) _`
• `Eq (Cardinality a)`
• `Ord (Cardinality a)`

### #EnumSource

``class (Ord a) <= Enum a  where``

Type class for enumerations.

Laws:

• `succ a > pred a`
• `pred a < succ a`
• `pred >=> succ >=> pred = pred`
• `succ >=> pred >=> succ = succ`

#### Members

• `succ :: a -> Maybe a`
• `pred :: a -> Maybe a`

#### Instances

• `Enum Boolean`
• `Enum Int`
• `Enum Char`
• `Enum Unit`
• `Enum Ordering`
• `(BoundedEnum a) => Enum (Maybe a)`
• `(BoundedEnum a, BoundedEnum b) => Enum (Either a b)`
• `(Enum a, BoundedEnum b) => Enum (Tuple a b)`

### #defaultSuccSource

``defaultSucc :: forall a. (Int -> Maybe a) -> (a -> Int) -> a -> Maybe a``

`defaultSucc toEnum fromEnum = succ`

### #defaultPredSource

``defaultPred :: forall a. (Int -> Maybe a) -> (a -> Int) -> a -> Maybe a``

`defaultPred toEnum fromEnum = pred`

### #enumFromToSource

``enumFromTo :: forall u a. Enum a => Unfoldable u => a -> a -> u a``

Returns a successive sequence of elements from the lower bound to the upper bound (inclusive).

### #enumFromThenToSource

``enumFromThenTo :: forall a. BoundedEnum a => a -> a -> a -> Array a``

`[a,b..c]`

### #upFromSource

``upFrom :: forall u a. Enum a => Unfoldable u => a -> u a``

### #downFromSource

``downFrom :: forall u a. Enum a => Unfoldable u => a -> u a``

### #BoundedEnumSource

``class (Bounded a, Enum a) <= BoundedEnum a  where``

Type class for finite enumerations.

This should not be considered a part of a numeric hierarchy, as in Haskell. Rather, this is a type class for small, ordered sum types with statically-determined cardinality and the ability to easily compute successor and predecessor elements, e.g. `DayOfWeek`.

Laws:

• `succ bottom >>= succ >>= succ ... succ [cardinality - 1 times] == top`
• `pred top >>= pred >>= pred ... pred [cardinality - 1 times] == bottom`
• `forall a > bottom: pred a >>= succ == Just a`
• `forall a < top: succ a >>= pred == Just a`
• `forall a > bottom: fromEnum <\$> pred a = Just (fromEnum a - 1)`
• `forall a < top: fromEnum <\$> succ a = Just (fromEnum a + 1)`
• `e1 `compare` e2 == fromEnum e1 `compare` fromEnum e2`
• `toEnum (fromEnum a) = Just a`

#### Members

• `cardinality :: Cardinality a`
• `toEnum :: Int -> Maybe a`
• `fromEnum :: a -> Int`

#### Instances

• `BoundedEnum Boolean`
• `BoundedEnum Char`
• `BoundedEnum Unit`
• `BoundedEnum Ordering`
• `(BoundedEnum a) => BoundedEnum (Maybe a)`
• `(BoundedEnum a, BoundedEnum b) => BoundedEnum (Either a b)`
• `(BoundedEnum a, BoundedEnum b) => BoundedEnum (Tuple a b)`

### #defaultCardinalitySource

``defaultCardinality :: forall a. Bounded a => Enum a => Cardinality a``

Runs in `O(n)` where `n` is `fromEnum top`

### #defaultToEnumSource

``defaultToEnum :: forall a. Bounded a => Enum a => Int -> Maybe a``

Runs in `O(n)` where `n` is `fromEnum a`

``defaultFromEnum :: forall a. Enum a => a -> Int``

Runs in `O(n)` where `n` is `fromEnum a`

### #toEnumWithDefaultsSource

``toEnumWithDefaults :: forall a. BoundedEnum a => a -> a -> Int -> a``

Like `toEnum` but returns the first argument if `x` is less than `fromEnum bottom` and the second argument if `x` is greater than `fromEnum top`.

``````toEnumWithDefaults False True (-1) -- False
toEnumWithDefaults False True 0    -- False
toEnumWithDefaults False True 1    -- True
toEnumWithDefaults False True 2    -- True
``````
Modules
Data.Enum