Module

Data.Enum

Package
purescript-enums
Repository
purescript/purescript-enums

#CardinalitySource

newtype Cardinality a

Constructors

Instances

#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

Instances

#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 a u. (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 a u. (Enum a, Unfoldable u) => a -> u a

#downFromSource

downFrom :: forall a u. (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

Instances

#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

#defaultFromEnumSource

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