1

For example, the groovy File class has a nice iterator that will filter out just directories and not files:

void eachDir(Closure closure) 

When I use eachDir, I have to use the verbose method of creating the collection first and appending to it:

def collection = []    
dir1.eachDir { dir ->
  collection << dir
}

Any way to get it back to the nice compact collect syntax?

2 Answers 2

3

I don't know of any "idiomatic" way of doing this, nice riddle! =D

You can try passing the eachDir, or any similar function, to a function that will collect its iterations:

def collectIterations(fn) {
    def col = []
    fn {
        col << it
    }
    col
}

And now you can use it as:

def dir = new File('/path/to/some/dir')
def subDirs = collectIterations(dir.&eachDir)

def file = new File('/path/to/some/file')
def lines = collectIterations(file.&eachLine) 

(that last example is equivalent to file.readLines())

And only for bonus points, you may define this function as a method in the Closure class:

Closure.metaClass.collectIterations = {->
    def col = []
    delegate.call {
        col << it
    }
    col
}

def dir = new File('/path/to/some/dir')
def subDirs = dir.&eachDir.collectIterations()

def file = new File('/path/to/some/file')
def lines = file.&eachLine.collectIterations()

Update: On the other hand, you might also do:

def col = []    
someDir.eachDir col.&add

Which I think is quite less convoluted, but it's not leveraging the collect method as you requested :)

1
  • Interesting approaches, update version is what I'll probably start using.
    – Josh Diehl
    Jan 4, 2012 at 4:03
1

Not for the specific example that you're talking about. File.eachDir is sort of a weird implementation IMO. It would have been nice if they implemented iterator() on File so that you could use the normal iterator methods on them rather than the custom built ones that just execute a closure.

The easiest way to get a clean one liner that does what you're looking for is to use listFiles instead combined with findAll:

dir1.listFiles().findAll { it.directory }

If you look at the implementation of eachDir, you'll see that it's doing this (and a whole lot more that you don't care about for this instance) under the covers.

For many similar situations, inject is the method that you'd be looking for to have a starting value that you change as you iterate through a collection:

def sum = [1, 2, 3, 4, 5].inject(0) { total, elem -> total + elem }
assert 15 == sum

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.