Skip to content

Commit d212fc3

Browse files
committed
Replace last with unsnoc
1 parent 9db2b21 commit d212fc3

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

System/Posix/DynamicLinker/Module.hsc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ withModule :: Maybe String
102102
withModule mdir file flags p = do
103103
let modPath = case mdir of
104104
Nothing -> file
105-
Just dir -> dir ++ if last dir == '/'
106-
then file
107-
else ('/':file)
105+
Just dir -> dir ++ case unsnoc dir of
106+
Just (_, '/') -> file
107+
Just{} -> '/' : file
108+
Nothing -> error "System.Posix.DynamicLinker.Module.withModule: directory should not be Just \"\", pass Nothing instead"
108109
modu <- moduleOpen modPath flags
109110
result <- p modu
110111
moduleClose modu
@@ -116,3 +117,10 @@ withModule_ :: Maybe String
116117
-> (Module -> IO a)
117118
-> IO ()
118119
withModule_ dir file flags p = withModule dir file flags p >>= \ _ -> return ()
120+
121+
-- Dual to 'Data.List.uncons'.
122+
unsnoc :: [a] -> Maybe ([a], a)
123+
unsnoc = foldr go Nothing
124+
where
125+
go x Nothing = Just ([], x)
126+
go x (Just (xs, lst)) = Just (x : xs, lst)

0 commit comments

Comments
 (0)