summaryrefslogtreecommitdiff
path: root/Utility/Misc.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2011-12-09 18:10:41 -0400
committerGravatar Joey Hess <joey@kitenet.net>2011-12-09 18:10:41 -0400
commit28699c95a7de284f07a5c0e34fb1755868301f3c (patch)
treeef6b372edf27c9cbb508169e7adf707dc25a84c6 /Utility/Misc.hs
parent95e748cbd4bb858a3b87621e60f5b43d53b50480 (diff)
some work on avoiding partial functions
There are still hundreds of places that use partial functions head, tail, init, and last.
Diffstat (limited to 'Utility/Misc.hs')
-rw-r--r--Utility/Misc.hs13
1 files changed, 13 insertions, 0 deletions
diff --git a/Utility/Misc.hs b/Utility/Misc.hs
index 728598723..541e150b7 100644
--- a/Utility/Misc.hs
+++ b/Utility/Misc.hs
@@ -27,6 +27,19 @@ readMaybe s = case reads s of
((x,_):_) -> Just x
_ -> Nothing
+{- Like break, but the character matching the condition is not included
+ - in the second result list.
+ -
+ - separate (== ':') "foo:bar" = ("foo", "bar")
+ - separate (== ':') "foobar" = ("foo, "")
+ -}
+separate :: (a -> Bool) -> [a] -> ([a], [a])
+separate c l = unbreak $ break c l
+ where
+ unbreak r@(a, b)
+ | null b = r
+ | otherwise = (a, tail b)
+
{- Catches IO errors and returns a Bool -}
catchBoolIO :: IO Bool -> IO Bool
catchBoolIO a = catchDefaultIO a False