[groovy-user] grep vs findAll

317 views
Skip to first unread message

Wujek Srujek

unread,
Dec 22, 2011, 5:52:04 AM12/22/11
to us...@groovy.codehaus.org
Hi. I am having some trouble finding any relevant difference between grep and findAll - it looks like grep is a more specific case, where the argument has its isCase method called for each instance in a collection, which can always be done with findAll as well. I can imagine that for example this:
list.grep(~/blah/)
is faster than
list.findAll { it ==~ /blah/ }
as it compiles the pattern only once (I suppose?), but otherwise I see no real difference.
Are there any conventions as to when you one and when the other? Or is any of these deprecated?

wujek

Dinko Srkoc

unread,
Dec 22, 2011, 6:31:24 AM12/22/11
to us...@groovy.codehaus.org
On 22 December 2011 11:52, Wujek Srujek <wujek....@googlemail.com> wrote:
> Hi. I am having some trouble finding any relevant difference between grep
> and findAll - it looks like grep is a more specific case, where the argument
> has its isCase method called for each instance in a collection, which can
> always be done with findAll as well. I can imagine that for example this:
> list.grep(~/blah/)
> is faster than
> list.findAll { it ==~ /blah/ }
> as it compiles the pattern only once (I suppose?), but otherwise I see no
> real difference.

Looking at the source, I'm not convinced `grep` is faster than
`findAll`. It has to go through MetaClass to invoke `isCase` on a
filter. There is no such step in `findAll`.

> Are there any conventions as to when you one and when the other? Or is any
> of these deprecated?

Personally, I use `findAll`, but, honestly, never gave it much thought.

Cheers,
Dinko

>
> wujek

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Reply all
Reply to author
Forward
0 new messages