Relude.Extra.Enum

relude-1.2.2.0: Safe, performant, user-friendly and lightweight Haskell Standard Library

Copyright | (c) 2018-2023 Kowainik
License | MIT
Maintainer | Kowainik <xrom.xkov@gmail.com>
Stability | Stable
Portability | Portable
Safe Haskell | Safe
Language | Haskell2010

Relude.Extra.Enum

Description

Mini `bounded-enum` framework inside `relude`.

Since: 0.1.0

Synopsis

- next :: (Eq a, Bounded a, Enum a) => a -> a
- prev :: (Eq a, Bounded a, Enum a) => a -> a
- safeToEnum :: (Bounded a, Enum a) => Int -> Maybe a

# Documentation

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

Like `succ`, but doesn't fail on `maxBound`. Instead it returns `minBound`.

    >>> next False
    True
    >>> next True
    False
    >>> succ True
    *** Exception: Prelude.Enum.Bool.succ: bad argument

Since: 0.1.0

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

Like `pred`, but doesn't fail on `minBound`. Instead it returns `maxBound`.

    >>> prev True
    False
    >>> prev False
    True
    >>> pred False
    *** Exception: Prelude.Enum.Bool.pred: bad argument

Since: 0.6.0.0

safeToEnum :: (Bounded a, Enum a) => Int -> Maybe a #

Returns `Nothing` if given `Int` outside range.

    >>> safeToEnum @Bool 0
    Just False
    >>> safeToEnum @Bool 1
    Just True
    >>> safeToEnum @Bool 2
    Nothing
    >>> safeToEnum @Bool (-1)
    Nothing

Since: 0.1.0