Skip to content

Commit 6d371f1

Browse files
committed
Implement unsnoc efficiently, in a single pass
1 parent ae4ad60 commit 6d371f1

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

System/FilePath/Internal.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ module System.OsPath.MODULE_NAME.Internal
112112

113113
{- HLINT ignore "Use fewer imports" -}
114114
import Prelude (Char, Bool(..), Maybe(..), (.), (&&), (<=), not, fst, maybe, (||), (==), ($), otherwise, fmap, mempty, (>=), (/=), (++), snd)
115+
import Data.Bifunctor (first)
115116
import Data.Semigroup ((<>))
116117
import qualified Prelude as P
117118
import Data.Maybe(isJust)
@@ -129,7 +130,6 @@ import Data.List(stripPrefix, isSuffixOf, uncons, dropWhileEnd)
129130
#else
130131
import Prelude (fromIntegral)
131132
import Control.Exception ( SomeException, evaluate, try, displayException )
132-
import Data.Bifunctor (first)
133133
import Control.DeepSeq (force)
134134
import GHC.IO (unsafePerformIO)
135135
import qualified Data.Char as C
@@ -1145,8 +1145,7 @@ stripSuffix xs ys = reverse P.<$> stripPrefix (reverse xs) (reverse ys)
11451145

11461146

11471147
unsnoc :: [a] -> Maybe ([a], a)
1148-
unsnoc [] = Nothing
1149-
unsnoc xs = Just (init xs, last xs)
1148+
unsnoc = L.foldr (\x -> Just . maybe ([], x) (first (x :))) Nothing
11501149

11511150

11521151
_period, _quotedbl, _backslash, _slash, _question, _U, _N, _C, _colon, _semicolon, _US, _less, _greater, _bar, _asterisk, _nul, _space, _underscore :: Char

0 commit comments

Comments
 (0)