Data.Enum
- Package
- purescript-enums
- Repository
- purescript/purescript-enums
#Cardinality Source
newtype Cardinality a
Constructors
Instances
Newtype (Cardinality a) _
Eq (Cardinality a)
Ord (Cardinality a)
#Enum Source
class (Ord a) <= Enum a where
Type 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 a
defaultSucc toEnum fromEnum = succ
#defaultPred Source
defaultPred :: forall a. (Int -> Maybe a) -> (a -> Int) -> a -> Maybe a
defaultPred toEnum fromEnum = pred
#enumFromTo Source
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).
#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 a
Results 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 a
Results 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 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 = pred (fromEnum a)
forall a < top: fromEnum <$> succ a = succ (fromEnum a)
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)
#defaultCardinality Source
defaultCardinality :: forall a. Bounded a => Enum a => Cardinality a
Runs in O(n)
where n
is fromEnum top
#defaultToEnum Source
defaultToEnum :: forall a. Bounded a => Enum a => Int -> Maybe a
Runs in O(n)
where n
is fromEnum a
#defaultFromEnum Source
defaultFromEnum :: forall a. Enum a => a -> Int
Runs in O(n)
where n
is fromEnum a
#toEnumWithDefaults Source
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 - Data.
Enum. Gen