[ 
https://issues.apache.org/jira/browse/GROOVY-8341?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16195651#comment-16195651
 ] 

Paul King commented on GROOVY-8341:
-----------------------------------

I think many people see linux find as always doing depth-first traversal 
(because it's definitely not breadth-first) of directories but -depth can be 
used to ensure children are processed before their parent (which is what we'd 
expect from a strict depth-first algorithm). Groovy {{eachDirRecurse}} is the 
same as linux find without the -depth option (give or take the difference with 
processing the root). You can get the strict behavior using {{traverse}} and 
it's {{postDir}} closure:
{code}
import static groovy.io.FileType.*
File rootDir = new File(new URI('file:/tmp/foo'))
rootDir.traverse(type: DIRECTORIES, postRoot: true, postDir: { println it }, {})
{code}
gives:
{noformat}
/tmp/foo/bar/baz
/tmp/foo/bar/foo
/tmp/foo/bar/bar
/tmp/foo/bar
/tmp/foo/baz
/tmp/foo
{noformat}
You could argue that a change in behavior is required but given the 
{{traverse}} functionality, I suspect we could perhaps just make the 
documentation clearer. What do you think?

> GDK: eachDirRecurse and eachFileRecurse are NOT depth-first
> -----------------------------------------------------------
>
>                 Key: GROOVY-8341
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8341
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-jdk
>    Affects Versions: 2.4.11
>         Environment: Windows Linux
>            Reporter: Dirk Heinrichs
>            Priority: Minor
>
> The documentation for those two methods states: "Sub-directories are 
> recursively searched in a depth-first fashion." But the result is NOT 
> depth-first, as the following example shows:
> Given a directory tree of {code}/tmp
> |-/foo
> | |-/bar
> |   |-/foo
> |   |-/bar
> |   |-/baz
> |-/baz{code}
> the following simple code {code}#!/usr/bin/groovy
> File rootDir = new File(new URI('file:/tmp/foo'))
> rootDir.eachDirRecurse {
>   println(it)
> }{code}
> gives this result {code}/tmp/foo/bar
> /tmp/foo/bar/baz
> /tmp/foo/bar/foo
> /tmp/foo/bar/bar
> /tmp/foo/baz{code}
> while {code}% find /tmp/foo -depth -type d{code}
> correctly gives {code}/tmp/foo/bar/baz
> /tmp/foo/bar/foo
> /tmp/foo/bar/bar
> /tmp/foo/bar
> /tmp/foo/baz
> /tmp/foo{code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to