From dc5ed8d3b677f3c45118f5d5a89ad528ede7b171 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 20 Dec 2011 11:01:50 -0400 Subject: amusing name This is both a partial Prelude that conflicts with the real one, and a way to guard against the Prelude's partial functions. --- Utility/BadPrelude.hs | 64 ----------------------------------------------- Utility/PartialPrelude.hs | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 64 deletions(-) delete mode 100644 Utility/BadPrelude.hs create mode 100644 Utility/PartialPrelude.hs (limited to 'Utility') diff --git a/Utility/BadPrelude.hs b/Utility/BadPrelude.hs deleted file mode 100644 index 04c9d9b0b..000000000 --- a/Utility/BadPrelude.hs +++ /dev/null @@ -1,64 +0,0 @@ -{- Some stuff from Prelude should not be used, as it tends to be a source - - of bugs. - - - - This exports functions that conflict with the prelude, which avoids - - them being accidentially used. - -} - -module Utility.BadPrelude where - -{- read should be avoided, as it throws an error - - Instead, use: readMaybe -} -read :: Read a => String -> a -read = Prelude.read - -{- head is a partial function; head [] is an error - - Instead, use: take 1 or headMaybe -} -head :: [a] -> a -head = Prelude.head - -{- tail is also partial - - Instead, use: drop 1 -} -tail :: [a] -> [a] -tail = Prelude.tail - -{- init too - - Instead, use: beginning -} -init :: [a] -> [a] -init = Prelude.init - -{- last too - - Instead, use: end or lastMaybe -} -last :: [a] -> a -last = Prelude.last - -{- Attempts to read a value from a String. - - - - Ignores leading/trailing whitespace, and throws away any trailing - - text after the part that can be read. - -} -readMaybe :: (Read a) => String -> Maybe a -readMaybe s = case reads s of - ((x,_):_) -> Just x - _ -> Nothing - -{- Like head but Nothing on empty list. -} -headMaybe :: [a] -> Maybe a -headMaybe [] = Nothing -headMaybe v = Just $ Prelude.head v - -{- Like last but Nothing on empty list. -} -lastMaybe :: [a] -> Maybe a -lastMaybe [] = Nothing -lastMaybe v = Just $ Prelude.last v - -{- All but the last element of a list. - - (Like init, but no error on an empty list.) -} -beginning :: [a] -> [a] -beginning [] = [] -beginning l = Prelude.init l - -{- Like last, but no error on an empty list. -} -end :: [a] -> [a] -end [] = [] -end l = [Prelude.last l] diff --git a/Utility/PartialPrelude.hs b/Utility/PartialPrelude.hs new file mode 100644 index 000000000..ad857196d --- /dev/null +++ b/Utility/PartialPrelude.hs @@ -0,0 +1,64 @@ +{- Parts of the Prelude are partial functions, which are a common source of + - bugs. + - + - This exports functions that conflict with the prelude, which avoids + - them being accidentially used. + -} + +module Utility.PartialPrelude where + +{- read should be avoided, as it throws an error + - Instead, use: readMaybe -} +read :: Read a => String -> a +read = Prelude.read + +{- head is a partial function; head [] is an error + - Instead, use: take 1 or headMaybe -} +head :: [a] -> a +head = Prelude.head + +{- tail is also partial + - Instead, use: drop 1 -} +tail :: [a] -> [a] +tail = Prelude.tail + +{- init too + - Instead, use: beginning -} +init :: [a] -> [a] +init = Prelude.init + +{- last too + - Instead, use: end or lastMaybe -} +last :: [a] -> a +last = Prelude.last + +{- Attempts to read a value from a String. + - + - Ignores leading/trailing whitespace, and throws away any trailing + - text after the part that can be read. + -} +readMaybe :: (Read a) => String -> Maybe a +readMaybe s = case reads s of + ((x,_):_) -> Just x + _ -> Nothing + +{- Like head but Nothing on empty list. -} +headMaybe :: [a] -> Maybe a +headMaybe [] = Nothing +headMaybe v = Just $ Prelude.head v + +{- Like last but Nothing on empty list. -} +lastMaybe :: [a] -> Maybe a +lastMaybe [] = Nothing +lastMaybe v = Just $ Prelude.last v + +{- All but the last element of a list. + - (Like init, but no error on an empty list.) -} +beginning :: [a] -> [a] +beginning [] = [] +beginning l = Prelude.init l + +{- Like last, but no error on an empty list. -} +end :: [a] -> [a] +end [] = [] +end l = [Prelude.last l] -- cgit v1.2.3