safe-0.3.9: Library of safe (exception free) functions

Safe Haskell | Safe
Language | Haskell98

Safe

Contents

- New functions
- Safe wrappers

Description

A module wrapping `Prelude`/`Data.List` functions that can throw exceptions, such as `head` and `!!`. Each unsafe function has up to four variants, e.g. with `tail`:

- ``tail` :: [a] -> [a]`, raises an error on `tail []`.
- ``tailMay` :: [a] -> Maybe [a]`, turns errors into `Nothing`.
- ``tailDef` :: [a] -> [a] -> [a]`, takes a default to return on errors.
- ``tailNote` :: String -> [a] -> [a]`, takes an extra argument which supplements the error message.
- ``tailSafe` :: [a] -> [a]`, returns some sensible default if possible, `[]` in the case of `tail`.

This module also introduces some new functions, documented at the top of the module.

Synopsis

- abort :: String -> a
- at :: [a] -> Int -> a
- lookupJust :: Eq a => a -> [(a, b)] -> b
- findJust :: (a -> Bool) -> [a] -> a
- elemIndexJust :: Eq a => a -> [a] -> Int
- findIndexJust :: (a -> Bool) -> [a] -> Int
- tailMay :: [a] -> Maybe [a]
- tailDef :: [a] -> [a] -> [a]
- tailNote :: String -> [a] -> [a]
- tailSafe :: [a] -> [a]
- initMay :: [a] -> Maybe [a]
- initDef :: [a] -> [a] -> [a]
- initNote :: String -> [a] -> [a]
- initSafe :: [a] -> [a]
- headMay :: [a] -> Maybe a
- headDef :: a -> [a] -> a
- headNote :: String -> [a] -> a
- lastMay :: [a] -> Maybe a
- lastDef :: a -> [a] -> a
- lastNote :: String -> [a] -> a
- minimumMay :: Ord a => [a] -> Maybe a
- minimumDef :: Ord a => a -> [a] -> a
- minimumNote :: Ord a => String -> [a] -> a
- maximumMay :: Ord a => [a] -> Maybe a
- maximumDef :: Ord a => a -> [a] -> a
- maximumNote :: Ord a => String -> [a] -> a
- minimumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a
- minimumByDef :: a -> (a -> a -> Ordering) -> [a] -> a
- minimumByNote :: String -> (a -> a -> Ordering) -> [a] -> a
- maximumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a
- maximumByDef :: a -> (a -> a -> Ordering) -> [a] -> a
- maximumByNote :: String -> (a -> a -> Ordering) -> [a] -> a
- foldr1May :: (a -> a -> a) -> [a] -> Maybe a
- foldr1Def :: a -> (a -> a -> a) -> [a] -> a
- foldr1Note :: String -> (a -> a -> a) -> [a] -> a
- foldl1May :: (a -> a -> a) -> [a] -> Maybe a
- foldl1Def :: a -> (a -> a -> a) -> [a] -> a
- foldl1Note :: String -> (a -> a -> a) -> [a] -> a
- foldl1May' :: (a -> a -> a) -> [a] -> Maybe a
- foldl1Def' :: a -> (a -> a -> a) -> [a] -> a
- foldl1Note' :: String -> (a -> a -> a) -> [a] -> a
- scanl1May :: (a -> a -> a) -> [a] -> Maybe [a]
- scanl1Def :: [a] -> (a -> a -> a) -> [a] -> [a]
- scanl1Note :: String -> (a -> a -> a) -> [a] -> [a]
- scanr1May :: (a -> a -> a) -> [a] -> Maybe [a]
- scanr1Def :: [a] -> (a -> a -> a) -> [a] -> [a]
- scanr1Note :: String -> (a -> a -> a) -> [a] -> [a]
- fromJustDef :: a -> Maybe a -> a
- fromJustNote :: String -> Maybe a -> a
- assertNote :: String -> Bool -> a -> a
- atMay :: [a] -> Int -> Maybe a
- atDef :: a -> [a] -> Int -> a
- atNote :: String -> [a] -> Int -> a
- readMay :: Read a => String -> Maybe a
- readDef :: Read a => a -> String -> a
- readNote :: Read a => String -> String -> a
- lookupJustDef :: Eq a => b -> a -> [(a, b)] -> b
- lookupJustNote :: Eq a => String -> a -> [(a, b)] -> b
- findJustDef :: a -> (a -> Bool) -> [a] -> a
- findJustNote :: String -> (a -> Bool) -> [a] -> a
- elemIndexJustDef :: Eq a => Int -> a -> [a] -> Int
- elemIndexJustNote :: Eq a => String -> a -> [a] -> Int
- findIndexJustDef :: Int -> (a -> Bool) -> [a] -> Int
- findIndexJustNote :: String -> (a -> Bool) -> [a] -> Int
- toEnumMay :: (Enum a, Bounded a) => Int -> Maybe a
- toEnumDef :: (Enum a, Bounded a) => a -> Int -> a
- toEnumNote :: (Enum a, Bounded a) => String -> Int -> a
- toEnumSafe :: (Enum a, Bounded a) => Int -> a

# New functions

abort :: String -> a Source

Synonym for `error`. Used for instances where the program has decided to exit because of invalid user input, or the user pressed quit etc. This function allows `error` to be reserved for programmer errors.

at :: [a] -> Int -> a Source

Synonym for `!!`, but includes more information in the error message.

lookupJust :: Eq a => a -> [(a, b)] -> b Source

    lookupJust key = fromJust . lookup key

findJust :: (a -> Bool) -> [a] -> a Source

    findJust op = fromJust . find op

elemIndexJust :: Eq a => a -> [a] -> Int Source

    elemIndexJust op = fromJust . elemIndex op

findIndexJust :: (a -> Bool) -> [a] -> Int Source

    findIndexJust op = fromJust . findIndex op

# Safe wrappers

tailMay :: [a] -> Maybe [a] Source

    tailMay [] = Nothing
    tailMay [1,3,4] = Just [3,4]

tailDef :: [a] -> [a] -> [a] Source

    tailDef [12] [] = [12]
    tailDef [12] [1,3,4] = [3,4]

tailNote :: String -> [a] -> [a] Source

    tailNote "help me" [] = error "Safe.tailNote [], help me"
    tailNote "help me" [1,3,4] = [3,4]

tailSafe :: [a] -> [a] Source

    tailSafe [] = []
    tailSafe [1,3,4] = [3,4]

initMay :: [a] -> Maybe [a] Source

initDef :: [a] -> [a] -> [a] Source

initNote :: String -> [a] -> [a] Source

initSafe :: [a] -> [a] Source

headMay :: [a] -> Maybe a Source

headDef :: a -> [a] -> a Source

headNote :: String -> [a] -> a Source

lastMay :: [a] -> Maybe a Source

lastDef :: a -> [a] -> a Source

lastNote :: String -> [a] -> a Source

minimumMay :: Ord a => [a] -> Maybe a Source

minimumDef :: Ord a => a -> [a] -> a Source

minimumNote :: Ord a => String -> [a] -> a Source

maximumMay :: Ord a => [a] -> Maybe a Source

maximumDef :: Ord a => a -> [a] -> a Source

maximumNote :: Ord a => String -> [a] -> a Source

minimumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a Source

minimumByDef :: a -> (a -> a -> Ordering) -> [a] -> a Source

minimumByNote :: String -> (a -> a -> Ordering) -> [a] -> a Source

maximumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a Source

maximumByDef :: a -> (a -> a -> Ordering) -> [a] -> a Source

maximumByNote :: String -> (a -> a -> Ordering) -> [a] -> a Source

foldr1May :: (a -> a -> a) -> [a] -> Maybe a Source

foldr1Def :: a -> (a -> a -> a) -> [a] -> a Source

foldr1Note :: String -> (a -> a -> a) -> [a] -> a Source

foldl1May :: (a -> a -> a) -> [a] -> Maybe a Source

foldl1Def :: a -> (a -> a -> a) -> [a] -> a Source

foldl1Note :: String -> (a -> a -> a) -> [a] -> a Source

foldl1May' :: (a -> a -> a) -> [a] -> Maybe a Source

foldl1Def' :: a -> (a -> a -> a) -> [a] -> a Source

foldl1Note' :: String -> (a -> a -> a) -> [a] -> a Source

scanl1May :: (a -> a -> a) -> [a] -> Maybe [a] Source

scanl1Def :: [a] -> (a -> a -> a) -> [a] -> [a] Source

scanl1Note :: String -> (a -> a -> a) -> [a] -> [a] Source

scanr1May :: (a -> a -> a) -> [a] -> Maybe [a] Source

scanr1Def :: [a] -> (a -> a -> a) -> [a] -> [a] Source

scanr1Note :: String -> (a -> a -> a) -> [a] -> [a] Source

fromJustDef :: a -> Maybe a -> a Source

An alternative name for `fromMaybe`, to fit the naming scheme of this package. Generally using `fromMaybe` directly would be considered better style.

fromJustNote :: String -> Maybe a -> a Source

assertNote :: String -> Bool -> a -> a Source

atMay :: [a] -> Int -> Maybe a Source

atDef :: a -> [a] -> Int -> a Source

atNote :: String -> [a] -> Int -> a Source

readMay :: Read a => String -> Maybe a Source

readDef :: Read a => a -> String -> a Source

readNote :: Read a => String -> String -> a Source

lookupJustDef :: Eq a => b -> a -> [(a, b)] -> b Source

lookupJustNote :: Eq a => String -> a -> [(a, b)] -> b Source

findJustDef :: a -> (a -> Bool) -> [a] -> a Source

findJustNote :: String -> (a -> Bool) -> [a] -> a Source

elemIndexJustDef :: Eq a => Int -> a -> [a] -> Int Source

elemIndexJustNote :: Eq a => String -> a -> [a] -> Int Source

findIndexJustDef :: Int -> (a -> Bool) -> [a] -> Int Source

findIndexJustNote :: String -> (a -> Bool) -> [a] -> Int Source

toEnumMay :: (Enum a, Bounded a) => Int -> Maybe a Source

toEnumDef :: (Enum a, Bounded a) => a -> Int -> a Source

toEnumNote :: (Enum a, Bounded a) => String -> Int -> a Source

toEnumSafe :: (Enum a, Bounded a) => Int -> a Source