summaryrefslogtreecommitdiff
path: root/compiler/utils/Util.lhs
diff options
context:
space:
mode:
authorDavid Feuer <David.Feuer@gmail.com>2014-10-01 23:34:29 +0200
committerJoachim Breitner <mail@joachim-breitner.de>2014-10-01 23:38:32 +0200
commit2a8856884de7d476e26b4ffa829ccb3a14d6f63e (patch)
treea5ba7fc47baf0cf68cf01336a38c963ad19c43b2 /compiler/utils/Util.lhs
parent53a2d46d185bcffe005e84b4e7acf6b196f2329e (diff)
downloadhaskell-2a8856884de7d476e26b4ffa829ccb3a14d6f63e.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/utils/Util.lhs')
-rw-r--r--compiler/utils/Util.lhs16
1 files changed, 15 insertions, 1 deletions
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}
%************************************************************************