diff options
author | David Feuer <David.Feuer@gmail.com> | 2014-10-01 23:34:29 +0200 |
---|---|---|
committer | Joachim Breitner <mail@joachim-breitner.de> | 2014-10-02 22:13:29 +0200 |
commit | 9bf5228fdc1937f44901a945553eea3cb0f14faa (patch) | |
tree | 243925bae6f8869cca1df8595c17c0467b9d5998 /compiler | |
parent | d6d5c127b86dc186b25add2843cb83fc12e72a85 (diff) | |
download | haskell-9bf5228fdc1937f44901a945553eea3cb0f14faa.tar.gz |
Use dropWhileEndLE p instead of reverse . dropWhile p . reverse
Summary: Using `dropWhileEndLE` tends to be faster and easier to read
than the `reverse . dropWhile p . reverse` idiom. This also cleans up
some other, nearby, messes. Fix #9616 (incorrect number formatting
potentially leading to incorrect numbers in output).
Test Plan: Run validate
Reviewers: thomie, rwbarton, nomeata, austin
Reviewed By: nomeata, austin
Subscribers: simonmar, ezyang, carter, thomie
Projects: #ghc
Differential Revision: https://phabricator.haskell.org/D259
GHC Trac Issues: #9623, #9616
Conflicts:
compiler/basicTypes/OccName.lhs
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/basicTypes/OccName.lhs | 2 | ||||
-rw-r--r-- | compiler/utils/Util.lhs | 16 |
2 files changed, 16 insertions, 2 deletions
diff --git a/compiler/basicTypes/OccName.lhs b/compiler/basicTypes/OccName.lhs index 1f1fda8ae3..0010ad37dc 100644 --- a/compiler/basicTypes/OccName.lhs +++ b/compiler/basicTypes/OccName.lhs @@ -833,7 +833,7 @@ tidyOccName env occ@(OccName occ_sp fs) Nothing -> (addToUFM env fs 1, occ) where base :: String -- Drop trailing digits (see Note [TidyOccEnv]) - base = reverse (dropWhile isDigit (reverse (unpackFS fs))) + base = dropWhileEndLE isDigit (unpackFS fs) find n = case lookupUFM env new_fs of diff --git a/compiler/utils/Util.lhs b/compiler/utils/Util.lhs index 7292b4a4b3..aa5f6f9c95 100644 --- a/compiler/utils/Util.lhs +++ b/compiler/utils/Util.lhs @@ -23,6 +23,8 @@ module Util ( mapAndUnzip, mapAndUnzip3, mapAccumL2, nOfThem, filterOut, partitionWith, splitEithers, + dropWhileEndLE, + foldl1', foldl2, count, all2, lengthExceeds, lengthIs, lengthAtLeast, @@ -593,6 +595,18 @@ dropTail n xs go _ _ = [] -- Stop when ys runs out -- It'll always run out before xs does +-- dropWhile from the end of a list. This is similar to Data.List.dropWhileEnd, +-- but is lazy in the elements and strict in the spine. For reasonably short lists, +-- such as path names and typical lines of text, dropWhileEndLE is generally +-- faster than dropWhileEnd. Its advantage is magnified when the predicate is +-- expensive--using dropWhileEndLE isSpace to strip the space off a line of text +-- is generally much faster than using dropWhileEnd isSpace for that purpose. +-- Specification: dropWhileEndLE p = reverse . dropWhile p . reverse +-- Pay attention to the short-circuit (&&)! The order of its arguments is the only +-- difference between dropWhileEnd and dropWhileEndLE. +dropWhileEndLE :: (a -> Bool) -> [a] -> [a] +dropWhileEndLE p = foldr (\x r -> if null r && p x then [] else x:r) [] + snocView :: [a] -> Maybe ([a],a) -- Split off the last element snocView [] = Nothing @@ -651,7 +665,7 @@ cmpList cmp (a:as) (b:bs) \begin{code} removeSpaces :: String -> String -removeSpaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace +removeSpaces = dropWhileEndLE isSpace . dropWhile isSpace \end{code} %************************************************************************ |