Skip to content

Commit 2059c96

Browse files
authored
Merge pull request #104 from haskell/avoid-init-and-last
Avoid partial functions Data.List.init and Data.List.last
2 parents ad8a676 + e632a23 commit 2059c96

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/Main.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ import HSCParser
6262
# endif
6363
#endif
6464

65+
#if MIN_VERSION_base(4,19,0)
66+
import Data.List (unsnoc)
67+
#else
68+
unsnoc :: [a] -> Maybe ([a], a)
69+
unsnoc = foldr (\x -> Just . maybe ([], x) (\(~(a, b)) -> (x : a, b))) Nothing
70+
#endif
71+
6572
#ifdef BUILD_NHC
6673
getDataFileName s = do here <- getCurrentDirectory
6774
return (here++"/"++s)
@@ -122,10 +129,9 @@ processFiles configM files usage = do
122129

123130
forM_ files (\name -> do
124131
(outName, outDir, outBase) <- case [f | Output f <- cFlags config] of
125-
[] -> if not (null ext) && last ext == 'c'
126-
then return (dir++base++init ext, dir, base)
127-
else
128-
if ext == ".hs"
132+
[] -> case unsnoc ext of
133+
Just (initExt, 'c') -> return (dir++base++initExt, dir, base)
134+
_ -> if ext == ".hs"
129135
then return (dir++base++"_out.hs", dir, base)
130136
else return (dir++base++".hs", dir, base)
131137
where

0 commit comments

Comments
 (0)