safe-0.3.4: Library for safe (pattern match free) functions

Safe HaskellSafe-Inferred

Safe

Description

A library for safe functions, based on standard functions that may crash.

In general, each unsafe function has up to 4 forms. Since tail has all the possible forms, it is fully documented. The others all follow the same pattern.

  • Note, takes an extra argument which supplements the error message, tailNote
  • Def, take an extra argument to give when a crash would otherwise happen, tailDef
  • May, wraps the result in a Maybe, tailMay
  • Safe, returns a default type if possible, tailSafe

This library also introduces three brand new functions:

  • at - synonym for (!!)
  • lookupJust - defined as lookupJust k = fromJust . lookup k
  • findJust - defined as findJust f = fromJust . find f
  • abort - same as error, but different intended meaning

Synopsis

Documentation

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

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

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

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

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

 tail "help me" [] = error "Pattern match failure, tail [], help me"
 tail "help me" [1,3,4] = [3,4]

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

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

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

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

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

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

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

headMay :: [a] -> Maybe aSource

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

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

lastMay :: [a] -> Maybe aSource

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

fromJustDef :: a -> Maybe a -> aSource

See fromMaybe

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

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

Same as (!!), but better error message

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

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

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

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

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

 lookupJust key = fromJust . lookup key

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

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

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

 findJust op = fromJust . find op

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

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

abort :: String -> aSource

Exactly the same as error. Use this for instances where the program has decided to exit because of invalid user input, or the user pressed quit etc. This allows error to be reserved for genuine coding mistakes.