[prev in list] [next in list] [prev in thread] [next in thread]
List: haskell-beginners
Subject: Re: [Haskell-beginners] Folders and sub-folders
From: Mike Houghton <mike_k_houghton () yahoo ! co ! uk>
Date: 2016-02-26 18:13:33
Message-ID: 6D1F3B6C-A168-4C94-BC17-1E1240B31DD6 () yahoo ! co ! uk
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
Cool, thanks.
> On 26 Feb 2016, at 13:01, Sylvain Henry <hsyl20@gmail.com> wrote:
>
> Your "isOk" function will filter out hidden directories on Unix (which may be what \
> you want?).
> Otherwise: isOk = (`notElem` [".",".."])
>
> Also "\x -> folders x" = "folders"
>
> 2016-02-26 11:42 GMT+01:00 Mike Houghton <mike_k_houghton@yahoo.co.uk \
> <mailto:mike_k_houghton@yahoo.co.uk>>: Thanks.
>
>
> I sweated it bit more and got
>
>
>
> isOk FilePath -> Bool
> isOk = not . isPrefixOf "."
>
> folders :: FilePath -> IO [FilePath]
> folders fp = do
> all <- getDirectoryContents fp
> z' <- filterM doesDirectoryExist $ map (fp </>) (filter isOk all)
> x' <- mapM (\x -> folders x) z'
> return $ z' ++ (concat x') ::
>
> which seems to work.
>
>
> > On 25 Feb 2016, at 19:07, Imants Cekusins <imantc@gmail.com \
> > <mailto:imantc@gmail.com>> wrote:
> > Hello Mike,
> >
> > below code find all files recursively from a starting point. It works.
> >
> > You'd need to tweak it to find folders instead.
> >
> >
> > import System.Directory
> > import Data.List
> >
> >
> > findAllFiles::FilePath -> IO [FilePath]
> > findAllFiles base0 = gd1 base0
> > > > = \list1 -> concatMap' recurse3 list1
> > where gd1 d1 = filter f2 <$> (getDirectoryContents d1)
> > f2 "." = False
> > f2 ".." = False
> > f2 _ = True
> > recurse3 md3 = doesDirectoryExist md3full
> > > > = \isDir3 ->
> > if isDir3 then findAllFiles md3full
> > else pure [md3full]
> > where md3full = base0 ++ "/" ++ md3
> >
> >
> >
> > concatMap':: (a -> IO [b]) -> [a] -> IO [b]
> > concatMap' m0 list0 = sequence (m0 <$> list0)
> > > > = \list2 -> pure $ concat list2
> > _______________________________________________
> > Beginners mailing list
> > Beginners@haskell.org <mailto:Beginners@haskell.org>
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners \
> > <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org <mailto:Beginners@haskell.org>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners \
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
[Attachment #5 (unknown)]
<html><head><meta http-equiv="Content-Type" content="text/html \
charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: \
space; -webkit-line-break: after-white-space;" class=""><div class="">Cool, \
thanks.</div><div class=""><br class=""></div><br class=""><div><blockquote \
type="cite" class=""><div class="">On 26 Feb 2016, at 13:01, Sylvain Henry <<a \
href="mailto:hsyl20@gmail.com" class="">hsyl20@gmail.com</a>> wrote:</div><br \
class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div \
class=""><div class="">Your "isOk" function will filter out hidden directories on \
Unix (which may be what you want?).<br class=""><br class=""></div>Otherwise: isOk = \
(`notElem` [".",".."])<br class=""><br class=""></div>Also "\x -> folders x" = \
"folders"<br class=""></div><div class="gmail_extra"><br class=""><div \
class="gmail_quote">2016-02-26 11:42 GMT+01:00 Mike Houghton <span dir="ltr" \
class=""><<a href="mailto:mike_k_houghton@yahoo.co.uk" target="_blank" \
class="">mike_k_houghton@yahoo.co.uk</a>></span>:<br class=""><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">Thanks.<br class=""> <br class="">
<br class="">
I sweated it bit more and got<br class="">
<br class="">
<br class="">
<br class="">
isOk FilePath -> Bool<br class="">
isOk = not . isPrefixOf "."<br class="">
<span class=""><br class="">
folders :: FilePath -> IO [FilePath]<br class="">
folders fp = do<br class="">
all <- getDirectoryContents fp<br class="">
</span> z' <- filterM doesDirectoryExist $ map (fp </>) (filter \
isOk all)<br class=""> x' <- mapM (\x -> folders x) z'<br \
class=""> return $ z' ++ (concat x') ::<br class="">
<br class="">
which seems to work.<br class="">
<div class="HOEnZb"><div class="h5"><br class="">
<br class="">
> On 25 Feb 2016, at 19:07, Imants Cekusins <<a href="mailto:imantc@gmail.com" \
class="">imantc@gmail.com</a>> wrote:<br class=""> ><br class="">
> Hello Mike,<br class="">
><br class="">
> below code find all files recursively from a starting point. It works.<br \
class=""> ><br class="">
> You'd need to tweak it to find folders instead.<br class="">
><br class="">
><br class="">
> import System.Directory<br class="">
> import Data.List<br class="">
><br class="">
><br class="">
> findAllFiles::FilePath -> IO [FilePath]<br class="">
> findAllFiles base0 = gd1 base0<br class="">
>>> = \list1 -> concatMap' recurse3 list1<br class="">
> where gd1 d1 = filter f2 <$> (getDirectoryContents \
d1)<br class=""> > f2 "." = False<br \
class=""> > f2 ".." = False<br class="">
> f2 _ = True<br class="">
> recurse3 md3 = doesDirectoryExist \
md3full<br class=""> >>> = \isDir3 -><br class="">
> \
if isDir3 then findAllFiles md3full<br class=""> > \
else pure [md3full]<br \
class=""> > \
where md3full = base0 ++ "/" ++ md3<br class=""> ><br class="">
><br class="">
><br class="">
> concatMap':: (a -> IO [b]) -> [a] -> IO [b]<br class="">
> concatMap' m0 list0 = sequence (m0 <$> list0)<br class="">
>>> = \list2 -> pure $ concat list2<br class="">
> _______________________________________________<br class="">
> Beginners mailing list<br class="">
> <a href="mailto:Beginners@haskell.org" class="">Beginners@haskell.org</a><br \
class=""> > <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" \
rel="noreferrer" target="_blank" \
class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br class=""> \
<br class=""> _______________________________________________<br class="">
Beginners mailing list<br class="">
<a href="mailto:Beginners@haskell.org" class="">Beginners@haskell.org</a><br \
class=""> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" \
rel="noreferrer" target="_blank" \
class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br class=""> \
</div></div></blockquote></div><br class=""></div> \
_______________________________________________<br class="">Beginners mailing list<br \
class=""><a href="mailto:Beginners@haskell.org" class="">Beginners@haskell.org</a><br \
class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners<br \
class=""></div></blockquote></div><br class=""></body></html>
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic