tomas-mrkvička
11/27/2023, 11:47 AMFileWalkDirection.BOTTOM_UP
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/-file-walk-direction/ should do the thing because I want to check files first and then go deep. But BOTTOM_UP
goes to directory first regardless its documentation: Depth-first search, directory is visited AFTER its files
. What am I missing here?
Thank you.ephemient
11/27/2023, 11:53 AMFile.walk
works as expected for me,
File(".").walk(FileWalkDirection.TOP_DOWN).first() == File(".")
File(".").walk(FileWalkDirection.BOTTOM_UP).last() == File(".")
tomas-mrkvička
11/27/2023, 12:58 PMfilter
is the one to blame.
File("/C:*/foo*").walkBottomUp().filter { file -> file.name == "gradle.properties" }.forEach { file -> println(file.absolutePath) }
prints:
C:\foo\xxx\gradle.properties
C:\foo\gradle.properties
C:\foo\asd\gradle.properties
C:\foo\zzz\gradle.properties
C:\foo\yyy\gradle.properties
So it looks like that filtering does something fishy with ordering.
In our real code we use .firstOrNull()
after filter {}
. So forEach {}
in my example is used to just print occurrences.ephemient
11/27/2023, 1:07 PMC:\foo\gradle.properties
and C:\foo\xxx
tomas-mrkvička
11/27/2023, 1:11 PMC:\foo\gradle.properties
is a file, it should go to the filter
method before C:\foo\xxx\gradle.properties
(because BOTTOM_UP
observers files first) and therefore should be first in the terminal operation.tomas-mrkvička
11/27/2023, 1:46 PMFile("/C:/foo").walkBottomUp().find { file -> file.name == "gradle.properties" }?.absolutePath)
also prints C:\foo\xxx\gradle.properties
as a winner even though there is a C:\foo\gradle.properties
in the root of C:/foo
directory.
Am I missing any fundamental Kotlin knowledge here?ephemient
11/27/2023, 3:16 PMephemient
11/27/2023, 3:23 PMA
/|\
B C D
|
E
in preorder (top-down), A is visited before BCD, but BCD have no order among themselves, and thus E may be visited either before or after CD (but always after B). in postorder (bottom-up), E is visited before B, and BCD is visited before A, but similarly there is no order among BCD so E may be visited before or after CD.tomas-mrkvička
11/27/2023, 4:08 PM