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

Safe HaskellSafe-Inferred

Safe

Contents

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

New functions

abort :: String -> aSource

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 -> aSource

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

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

 lookupJust key = fromJust . lookup key

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

 findJust op = fromJust . find op

elemIndexJust :: Eq a => a -> [a] -> IntSource

 elemIndexJust op = fromJust . elemIndex op

findIndexJust :: (a -> Bool) -> [a] -> IntSource

 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 aSource

headDef :: a -> [a] -> aSource

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

lastMay :: [a] -> Maybe aSource

lastDef :: a -> [a] -> aSource

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 -> aSource

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

assertNote :: String -> Bool -> a -> aSource

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

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

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

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

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

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

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

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

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

elemIndexJustNote :: Eq a => String -> a -> [a] -> IntSource

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

findIndexJustNote :: String -> (a -> Bool) -> [a] -> IntSource