[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 &lt;<a \
href="mailto:hsyl20@gmail.com" class="">hsyl20@gmail.com</a>&gt; 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 -&gt; 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="">&lt;<a href="mailto:mike_k_houghton@yahoo.co.uk" target="_blank" \
class="">mike_k_houghton@yahoo.co.uk</a>&gt;</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&nbsp; bit more and got<br class="">
<br class="">
<br class="">
<br class="">
isOk FilePath -&gt; Bool<br class="">
isOk&nbsp; = not . isPrefixOf "."<br class="">
<span class=""><br class="">
folders :: FilePath -&gt; IO [FilePath]<br class="">
folders fp&nbsp; = do<br class="">
&nbsp; &nbsp; all &lt;- getDirectoryContents fp<br class="">
</span>&nbsp; &nbsp; z' &lt;- filterM doesDirectoryExist $ map (fp &lt;/&gt;) (filter \
isOk all)<br class=""> &nbsp; &nbsp; x' &lt;- mapM (\x -&gt; folders x) z'<br \
class=""> &nbsp; &nbsp; return $ z' ++ (concat x') ::<br class="">
<br class="">
which seems to work.<br class="">
<div class="HOEnZb"><div class="h5"><br class="">
<br class="">
&gt; On 25 Feb 2016, at 19:07, Imants Cekusins &lt;<a href="mailto:imantc@gmail.com" \
class="">imantc@gmail.com</a>&gt; wrote:<br class=""> &gt;<br class="">
&gt; Hello Mike,<br class="">
&gt;<br class="">
&gt; below code find all files recursively from a starting point. It works.<br \
class=""> &gt;<br class="">
&gt; You'd need to tweak it to find folders instead.<br class="">
&gt;<br class="">
&gt;<br class="">
&gt; import System.Directory<br class="">
&gt; import Data.List<br class="">
&gt;<br class="">
&gt;<br class="">
&gt; findAllFiles::FilePath -&gt; IO [FilePath]<br class="">
&gt; findAllFiles base0 = gd1 base0<br class="">
&gt;&gt;&gt; = \list1 -&gt; concatMap' recurse3 list1<br class="">
&gt;&nbsp; &nbsp; &nbsp;where gd1 d1 = filter f2 &lt;$&gt; (getDirectoryContents \
d1)<br class=""> &gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f2 "." = False<br \
class=""> &gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f2 ".." = False<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f2 _ = True<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;recurse3 md3 = doesDirectoryExist \
md3full<br class=""> &gt;&gt;&gt; = \isDir3 -&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; if isDir3 then findAllFiles md3full<br class=""> &gt;&nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else pure [md3full]<br \
class=""> &gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
where md3full = base0 ++ "/" ++ md3<br class=""> &gt;<br class="">
&gt;<br class="">
&gt;<br class="">
&gt; concatMap':: (a -&gt; IO [b]) -&gt; [a] -&gt; IO [b]<br class="">
&gt; concatMap' m0 list0 = sequence (m0 &lt;$&gt; list0)<br class="">
&gt;&gt;&gt; = \list2 -&gt; pure $ concat list2<br class="">
&gt; _______________________________________________<br class="">
&gt; Beginners mailing list<br class="">
&gt; <a href="mailto:Beginners@haskell.org" class="">Beginners@haskell.org</a><br \
class=""> &gt; <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