diff options
Diffstat (limited to 'compiler/utils/Util.hs')
-rw-r--r-- | compiler/utils/Util.hs | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/compiler/utils/Util.hs b/compiler/utils/Util.hs index 0c7bb4a189..876cd1ee6e 100644 --- a/compiler/utils/Util.hs +++ b/compiler/utils/Util.hs @@ -28,7 +28,7 @@ module Util ( mapAndUnzip, mapAndUnzip3, mapAccumL2, nOfThem, filterOut, partitionWith, - dropWhileEndLE, spanEnd, last2, + dropWhileEndLE, spanEnd, last2, lastMaybe, foldl1', foldl2, count, countWhile, all2, @@ -779,6 +779,15 @@ last2 = foldl' (\(_,x2) x -> (x2,x)) (partialError,partialError) where partialError = panic "last2 - list length less than two" +lastMaybe :: [a] -> Maybe a +lastMaybe [] = Nothing +lastMaybe xs = Just $ last xs + +-- | If there is a good chance that you will only look at the last +-- element prefer seperate calls to @last@ + @init@. +-- @last@ does not allocate while traversing the list, while this +-- will. But if you are guaranteed to use both this will +-- usually be more efficient. snocView :: [a] -> Maybe ([a],a) -- Split off the last element snocView [] = Nothing |