- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
File System Traversal
Wed, 2012-02-08, 01:38
I got some nice code a while ago from Joshua Suereth which included
// We create a 'collection' that can iterate over files.
object files extends Traversable[File] {
override def foreach[U](f: File => U): Unit = {
// Note we could remove recursion here with some work. You
should make this recurse in whatever order you
// desire for optimum early-exit in your algorithm.
def iter(file: File) : Unit =
if(file.isDirectory) file.listFiles foreach iter
else f(file)
iter(new File(sourceName))
}
}
I was just wondering if there is already something in the Scala
libraries that does this as it seems like a really common thing to do -
traverse a set of files and directories.
Scala often seems to have nice little utilities for basic things like
this, but I am not always sure of the best way to find them. For
example, it would be nice to just do something like
val scalaFiles = File("src").traversable.view.filter(_.getName
endsWith ".scala")
But Scala does not seem to have its own file system API, nor can I find
anything suitable in Scalaz.
I guess there is the basic question of trade-offs: how much stuff should
people just know how to do in Scala because it is easy to express (like
Josh's code) and how much should be found in the libraries because it is
a common and simple operation.
Cheers, Eric
Wed, 2012-02-08, 03:21
#2
Re: File System Traversal
In particular, on Files API, there's stuff like this:
http://jesseeichar.github.com/scala-io-doc/0.3.0/index.html#!/file/descendant_processing
http://jesseeichar.github.com/scala-io-doc/0.3.0/index.html#!/file/descendants_using_path_sets
Note that traversable are single dimension structures, while file
systems are trees at best, and disjunctive cyclical graphs at worst.
On Wed, Feb 8, 2012 at 00:02, Daniel Sobral wrote:
> On Tue, Feb 7, 2012 at 22:39, Eric Kolotyluk wrote:
>> I got some nice code a while ago from Joshua Suereth which included
>>
>> // We create a 'collection' that can iterate over files.
>> object files extends Traversable[File] {
>> override def foreach[U](f: File => U): Unit = {
>> // Note we could remove recursion here with some work. You should
>> make this recurse in whatever order you
>> // desire for optimum early-exit in your algorithm.
>> def iter(file: File) : Unit =
>> if(file.isDirectory) file.listFiles foreach iter
>> else f(file)
>> iter(new File(sourceName))
>> }
>> }
>>
>> I was just wondering if there is already something in the Scala libraries
>> that does this as it seems like a really common thing to do - traverse a set
>> of files and directories.
>>
>> Scala often seems to have nice little utilities for basic things like this,
>> but I am not always sure of the best way to find them. For example, it would
>> be nice to just do something like
>>
>> val scalaFiles = File("src").traversable.view.filter(_.getName endsWith
>> ".scala")
>>
>> But Scala does not seem to have its own file system API, nor can I find
>> anything suitable in Scalaz.
>>
>> I guess there is the basic question of trade-offs: how much stuff should
>> people just know how to do in Scala because it is easy to express (like
>> Josh's code) and how much should be found in the libraries because it is a
>> common and simple operation.
>
> I haven't looked at it in a long while, but check
> http://jesseeichar.github.com/scala-io-doc/0.3.0/index.html#!/overview.
>
>
> --
> Daniel C. Sobral
>
> I travel to the future all the time.
Wed, 2012-02-08, 08:41
#3
Re: File System Traversal
On 2012-02-07 6:06 PM, Daniel Sobral wrote:
> In particular, on Files API, there's stuff like this:
>
> http://jesseeichar.github.com/scala-io-doc/0.3.0/index.html#!/file/descendant_processing
> http://jesseeichar.github.com/scala-io-doc/0.3.0/index.html#!/file/descendants_using_path_sets
>
> Note that traversable are single dimension structures, while file
> systems are trees at best, and disjunctive cyclical graphs at worst.
In this case what I really want is a single dimension structure because
I am just filtering files, and I don't really care about any other
structure. However, I really don't want to get in a loop or visit the
same file twice, so thanks for pointing that out.
>
> On Wed, Feb 8, 2012 at 00:02, Daniel Sobral wrote:
>> On Tue, Feb 7, 2012 at 22:39, Eric Kolotyluk wrote:
>>> I got some nice code a while ago from Joshua Suereth which included
>>>
>>> // We create a 'collection' that can iterate over files.
>>> object files extends Traversable[File] {
>>> override def foreach[U](f: File => U): Unit = {
>>> // Note we could remove recursion here with some work. You should
>>> make this recurse in whatever order you
>>> // desire for optimum early-exit in your algorithm.
>>> def iter(file: File) : Unit =
>>> if(file.isDirectory) file.listFiles foreach iter
>>> else f(file)
>>> iter(new File(sourceName))
>>> }
>>> }
>>>
>>> I was just wondering if there is already something in the Scala libraries
>>> that does this as it seems like a really common thing to do - traverse a set
>>> of files and directories.
>>>
>>> Scala often seems to have nice little utilities for basic things like this,
>>> but I am not always sure of the best way to find them. For example, it would
>>> be nice to just do something like
>>>
>>> val scalaFiles = File("src").traversable.view.filter(_.getName endsWith
>>> ".scala")
>>>
>>> But Scala does not seem to have its own file system API, nor can I find
>>> anything suitable in Scalaz.
>>>
>>> I guess there is the basic question of trade-offs: how much stuff should
>>> people just know how to do in Scala because it is easy to express (like
>>> Josh's code) and how much should be found in the libraries because it is a
>>> common and simple operation.
>> I haven't looked at it in a long while, but check
>> http://jesseeichar.github.com/scala-io-doc/0.3.0/index.html#!/overview.
>>
>>
>> --
>> Daniel C. Sobral
>>
>> I travel to the future all the time.
>
>
On Tue, Feb 7, 2012 at 22:39, Eric Kolotyluk wrote:
> I got some nice code a while ago from Joshua Suereth which included
>
> // We create a 'collection' that can iterate over files.
> object files extends Traversable[File] {
> override def foreach[U](f: File => U): Unit = {
> // Note we could remove recursion here with some work. You should
> make this recurse in whatever order you
> // desire for optimum early-exit in your algorithm.
> def iter(file: File) : Unit =
> if(file.isDirectory) file.listFiles foreach iter
> else f(file)
> iter(new File(sourceName))
> }
> }
>
> I was just wondering if there is already something in the Scala libraries
> that does this as it seems like a really common thing to do - traverse a set
> of files and directories.
>
> Scala often seems to have nice little utilities for basic things like this,
> but I am not always sure of the best way to find them. For example, it would
> be nice to just do something like
>
> val scalaFiles = File("src").traversable.view.filter(_.getName endsWith
> ".scala")
>
> But Scala does not seem to have its own file system API, nor can I find
> anything suitable in Scalaz.
>
> I guess there is the basic question of trade-offs: how much stuff should
> people just know how to do in Scala because it is easy to express (like
> Josh's code) and how much should be found in the libraries because it is a
> common and simple operation.
I haven't looked at it in a long while, but check
http://jesseeichar.github.com/scala-io-doc/0.3.0/index.html#!/overview.