Data.Enum
- Package
- purescript-enums
- Repository
- purescript/purescript-enums
#Cardinality Source
newtype Cardinality aConstructors
Instances
Newtype (Cardinality a) _Eq (Cardinality a)Ord (Cardinality a)
#Enum Source
class (Ord a) <= Enum a whereType class for enumerations.
Laws:
- Successor:
all (a < _) (succ a) - Predecessor:
all (_ < a) (pred a) - Succ retracts pred:
pred >=> succ >=> pred = pred - Pred retracts succ:
succ >=> pred >=> succ = succ - Non-skipping succ:
b <= a || any (_ <= b) (succ a) - Non-skipping pred:
a <= b || any (b <= _) (pred a)
The retraction laws can intuitively be understood as saying that succ is
the opposite of pred; if you apply succ and then pred to something,
you should end up with what you started with (although of course this
doesn't apply if you tried to succ the last value in an enumeration and
therefore got Nothing out). The non-skipping laws can intuitively be
understood as saying that succ shouldn't skip over any elements of your
type. For example, without the non-skipping laws, it would be permissible
to write an Enum Int instance where succ x = Just (x+2), and similarly
pred x = Just (x-2).
Members
Instances
#defaultSucc Source
defaultSucc :: forall a. (Int -> Maybe a) -> (a -> Int) -> a -> Maybe adefaultSucc toEnum fromEnum = succ
#defaultPred Source
defaultPred :: forall a. (Int -> Maybe a) -> (a -> Int) -> a -> Maybe adefaultPred toEnum fromEnum = pred
#enumFromTo Source
enumFromTo :: forall u a. Enum a => Unfoldable u => a -> a -> u aReturns a successive sequence of elements from the lower bound to the upper bound (inclusive).
#enumFromThenTo Source
enumFromThenTo :: forall a. BoundedEnum a => a -> a -> a -> Array a[a,b..c]
#upFrom Source
upFrom :: forall u a. Enum a => Unfoldable u => a -> u aResults in all successors from given Enum in some unfoldable. Note that given Enum is not included in the result.
#upFromIncluding Source
upFromIncluding :: forall u a. Enum a => Unfoldable u => a -> NonEmpty u aResults in all successors of given Enum (including itself) in some NonEmpty unfoldable.
#downFrom Source
downFrom :: forall u a. Enum a => Unfoldable u => a -> u a#BoundedEnum Source
class (Bounded a, Enum a) <= BoundedEnum a whereType 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] == toppred top >>= pred >>= pred ... pred [cardinality - 1 times] == bottomforall a > bottom: pred a >>= succ == Just aforall a < top: succ a >>= pred == Just aforall a > bottom: fromEnum <$> pred a = pred (fromEnum a)forall a < top: fromEnum <$> succ a = succ (fromEnum a)e1 `compare` e2 == fromEnum e1 `compare` fromEnum e2toEnum (fromEnum a) = Just a
Members
cardinality :: Cardinality atoEnum :: Int -> Maybe afromEnum :: a -> Int
Instances
BoundedEnum BooleanBoundedEnum CharBoundedEnum UnitBoundedEnum Ordering(BoundedEnum a) => BoundedEnum (Maybe a)(BoundedEnum a, BoundedEnum b) => BoundedEnum (Either a b)(BoundedEnum a, BoundedEnum b) => BoundedEnum (Tuple a b)
#defaultCardinality Source
defaultCardinality :: forall a. Bounded a => Enum a => Cardinality aRuns in O(n) where n is fromEnum top
#defaultToEnum Source
defaultToEnum :: forall a. Bounded a => Enum a => Int -> Maybe aRuns in O(n) where n is fromEnum a
#defaultFromEnum Source
defaultFromEnum :: forall a. Enum a => a -> IntRuns in O(n) where n is fromEnum a
#toEnumWithDefaults Source
toEnumWithDefaults :: forall a. BoundedEnum a => a -> a -> Int -> aLike 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 - Data.
Enum. Gen