Simple I'm sure.. best way to grab the first three items from a collection

View: New views
5 Messages — Rating Filter:   Alert me  

Simple I'm sure.. best way to grab the first three items from a collection

by Rick R-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This should be so simple, but I'm stumped on the best way to do it.

Have a collection of x number of Foo items. I want the first three from this collection.

Thanks an advance. I know the answer is going to be a "duh. why didn't I think of that" moment.

--
Rick R

Re: Simple I'm sure.. best way to grab the first three items from a collection

by Jeff Philp :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Would the simplest way be to use a range:

e.g

a = [ Foo1, Foo2, Foo3, Foo4, Foo5]
a[0..2] will return a collection of the first three Foo elements.

Jeffrey

2009/11/6 Rick <rickcr@...>:

> This should be so simple, but I'm stumped on the best way to do it.
>
> Have a collection of x number of Foo items. I want the first three from this
> collection.
>
> Thanks an advance. I know the answer is going to be a "duh. why didn't I
> think of that" moment.
>
> --
> Rick R
>

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

    http://xircles.codehaus.org/manage_email



Re: Simple I'm sure.. best way to grab the first three items from a collection

by Rick R-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Thu, Nov 5, 2009 at 5:27 PM, Jeff Philp <offstage@...> wrote:
Would the simplest way be to use a range:

e.g

a = [ Foo1, Foo2, Foo3, Foo4, Foo5]
a[0..2] will return a collection of the first three Foo elements.
 

perfect thanks Jeff! I knew there had to be something better than:

def items = [1,2,3,4,5]
def col = []
(0..2).each {
    col[it] = items[it]
}

What's interesting is I didn't see that syntax you posted anywhere in the docs? or on the collection page http://groovy.codehaus.org/Collections

I knew I saw what you posted before but couldn't recall it from memory.

RE: Simple I'm sure.. best way to grab the first three items from a collection

by Bob Brown-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

A case could be made for a bit of "defensive programming" here:

def a = [ 1, 2 ]
a[0..<Math.min(3, a.size())]

Cheers,

BOB

> -----Original Message-----
> From: Rick [mailto:rickcr@...]
> Sent: Friday, 6 November 2009 8:24 AM
> To: Groovy Users List
> Subject: [groovy-user] Simple I'm sure.. best way to grab the first
> three items from a collection
>
> This should be so simple, but I'm stumped on the best way to do it.
>
> Have a collection of x number of Foo items. I want the first three from
> this collection.
>
> Thanks an advance. I know the answer is going to be a "duh. why didn't
> I think of that" moment.
>
> --
> Rick R



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

    http://xircles.codehaus.org/manage_email



RE: Simple I'm sure.. best way to grab the first three items from a collection

by Bob Brown-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The thought occurs to me...how about 'codifying' this behaviour?

I propose something like:

def a = [ 1, 2 ]
a[0..>3]

Which would introduce a size-aware range subscript...

Thoughts/Comments?

BOB

> -----Original Message-----
> From: Bob Brown [mailto:bob@...]
> Sent: Friday, 6 November 2009 8:40 AM
> To: user@...
> Subject: RE: [groovy-user] Simple I'm sure.. best way to grab the first
> three items from a collection
>
> A case could be made for a bit of "defensive programming" here:
>
> def a = [ 1, 2 ]
> a[0..<Math.min(3, a.size())]
>
> Cheers,
>
> BOB
>
> > -----Original Message-----
> > From: Rick [mailto:rickcr@...]
> > Sent: Friday, 6 November 2009 8:24 AM
> > To: Groovy Users List
> > Subject: [groovy-user] Simple I'm sure.. best way to grab the first
> > three items from a collection
> >
> > This should be so simple, but I'm stumped on the best way to do it.
> >
> > Have a collection of x number of Foo items. I want the first three
> from
> > this collection.
> >
> > Thanks an advance. I know the answer is going to be a "duh. why
> didn't
> > I think of that" moment.
> >
> > --
> > Rick R
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>



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

    http://xircles.codehaus.org/manage_email