Loading [MathJax]/jax/output/HTML-CSS/jax.js

relude-0.2.0: Custom prelude from Kowainik

Copyright(c) 2018 Kowainik
LicenseMIT
MaintainerKowainik <xrom.xkov@gmail.com>
Safe HaskellSafe
LanguageHaskell2010

Relude.Extra.Enum

Description

Mini bounded-enum framework inside relude.

Synopsis

Documentation

universe :: (Bounded a, Enum a) => [a] Source #

Returns all values of some Bounded Enum in ascending order.

>>> data TrafficLight = Red | Blue | Green deriving (Show, Enum, Bounded)
>>> universe :: [TrafficLight]
[Red,Blue,Green]
>>> universe :: [Bool]
[False,True]

inverseMap :: forall a k. (Bounded a, Enum a, Ord k) => (a -> k) -> k -> Maybe a Source #

inverseMap f creates a function that is the inverse of a given function f. It does so by constructing Map for every value f a. The implementation makes sure that the Map is constructed only once and then shared for every call.

The complexity of reversed mapping though is O(logn).

Usually you want to use inverseMap to inverse show function.

>>> data Color = Red | Green | Blue deriving (Show, Enum, Bounded)
>>> parse = inverseMap show :: String -> Maybe Color
>>> parse "Red"
Just Red
>>> parse "Black"
Nothing

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

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

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

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

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

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

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