Skip to content

Commit 13d25ae

Browse files
fix bugs in filterM
1 parent dabba7f commit 13d25ae

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/System/File/Tree.hs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ mkFSTree a = FSTree . Node a . mapToTree
127127
-- |Efficiently maps 'FSTree' over a list. This is more efficient than map FSTree
128128
mapFSTree :: Forest FilePath -> FSForest
129129
mapFSTree = unsafeCoerce
130+
{-# NOINLINE mapFSTree #-}
130131

131132
-- |Efficiently maps toTree over a list. This is more effficient than map toTree
132133
mapToTree :: FSForest -> Forest FilePath
133134
mapToTree = unsafeCoerce
135+
{-# NOINLINE mapToTree #-}
134136

135137
-- |Overloaded lenses for 'Tree' and 'FSTree'
136138
class TreeLens t a | t -> a where
@@ -156,6 +158,7 @@ instance TreeLens FSTree FilePath where
156158
-- current directories and relative paths apply to the tree as a whole.
157159
getDirectory :: FilePath -> IO FSTree
158160
getDirectory = getDir_ unsafeInterleaveIO
161+
{-# NOINLINE getDirectory #-}
159162

160163
-- |A strict variant of 'getDirectory'.
161164
--
@@ -277,16 +280,18 @@ extract p = runIdentity . extractM (return . p)
277280
-- |Monadic 'filter'.
278281
filterM :: Monad m =>
279282
(FilePath -> m Bool) -> FSForest -> m FSForest
280-
filterM p = foldrM (filter' . prependPaths) []
281-
where filter' (Node path cs) ts =
282-
ifM (p path)
283-
(liftM ((:ts) . mkFSTree path) $ foldrM filter' [] cs)
284-
(do
285-
cs' <- foldrM filter' [] $ cs
286-
return $ case cs' of
287-
[] -> ts
288-
_ -> mkFSTree path cs' : ts
289-
)
283+
filterM p = foldrM (filter' "") [] . mapToTree
284+
where
285+
filter' d (Node file cs) ts = do
286+
let path = d </> file
287+
cs' <- foldrM (filter' path) [] cs
288+
b <- p path
289+
return $
290+
if b
291+
then mkFSTree file cs' : ts
292+
else case cs' of
293+
[] -> ts
294+
_ -> mkFSTree file cs' : ts
290295

291296
-- |Monadic 'find'.
292297
findM :: Monad m =>
@@ -450,7 +455,7 @@ zipWithDestM__ :: Monad m =>
450455
-> FilePath -> FSTree
451456
-> m ([a], FSTree)
452457
zipWithDestM__ f rootDest fs =
453-
liftM2 (,) (sequence $ (zipWith f `on` flatten) fs destFs)
458+
liftM2 (,) (sequence $ zipWith f (flatten fs) (flatten destFs))
454459
(return destFs)
455460
where
456461
destFs = setL label rootDest fs

0 commit comments

Comments
 (0)