Skip to content

Commit ae4ad60

Browse files
committed
Implement spanEnd efficiently: in a single pass and without reverses
1 parent 05bed83 commit ae4ad60

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

System/FilePath/Internal.hs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ import qualified Data.List as L
120120
#ifndef OS_PATH
121121
import Data.String (fromString)
122122
import System.Environment(getEnv)
123-
import Prelude (String, map, FilePath, Eq, IO, id, last, init, reverse, dropWhile, null, break, takeWhile, take, all, elem, any, span)
123+
import Prelude (String, map, FilePath, Eq, IO, id, last, init, reverse, dropWhile, null, break, take, all, elem, any, span)
124124
import Data.Char(toLower, toUpper, isAsciiLower, isAsciiUpper)
125125
import Data.List(stripPrefix, isSuffixOf, uncons, dropWhileEnd)
126126
#define CHAR Char
@@ -1129,13 +1129,9 @@ isAbsolute = not . isRelative
11291129
#ifndef OS_PATH
11301130

11311131
-----------------------------------------------------------------------------
1132-
-- takeWhileEnd (>2) [1,2,3,4,1,2,3,4] == [3,4])
1133-
takeWhileEnd :: (a -> Bool) -> [a] -> [a]
1134-
takeWhileEnd p = reverse . takeWhile p . reverse
1135-
11361132
-- spanEnd (>2) [1,2,3,4,1,2,3,4] = ([1,2,3,4,1,2], [3,4])
11371133
spanEnd :: (a -> Bool) -> [a] -> ([a], [a])
1138-
spanEnd p xs = (dropWhileEnd p xs, takeWhileEnd p xs)
1134+
spanEnd p = L.foldr (\x (pref, suff) -> if null pref && p x then (pref, x : suff) else (x : pref, suff)) ([], [])
11391135

11401136
-- breakEnd (< 2) [1,2,3,4,1,2,3,4] == ([1,2,3,4,1],[2,3,4])
11411137
breakEnd :: (a -> Bool) -> [a] -> ([a], [a])

0 commit comments

Comments
 (0)