Truncate an array / list?

1,423 views
Skip to first unread message

dw

unread,
Dec 9, 2010, 2:50:36 AM12/9/10
to gosu-lang
Silly question, but I'm wondering what the best way is to remove the
first N elements of an array or list. I didn't see anything obvious
in the docs.

Cheers,

David

Brandon Gresham

unread,
Dec 9, 2010, 9:47:56 AM12/9/10
to gosu...@googlegroups.com
You could use java's ArrayList...

ArrayList.removeRange( 0, N )


For arrays, you could massage to an ArrayList first...

var arrayList = Arrays.asList( myArray ) as ArrayList

Cheers,

David

--
You received this message because you are subscribed to the Google Groups
"gosu-lang" group.
To post to this group, send email to gosu...@googlegroups.com.
To unsubscribe from this group, send email to
gosu-lang+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/gosu-lang?hl=en.


Carson Gross

unread,
Dec 9, 2010, 1:26:18 PM12/9/10
to gosu...@googlegroups.com
On List, the subList() method returns a "view" of the larger list, so modifications will percolate through to it:


  var myList = {"a", "b", "c}
  myList.subList( 0, 2 ).clear()
  print( myList ) // prints ["c"]

Cheers,
Carson

Y Tso-Guillen

unread,
Dec 9, 2010, 8:23:44 PM12/9/10
to gosu-lang
If you don't care to mutate the list in any way, you can try this:

var myList = {"a", "b", "c", "d", "e", "f", "g"}
return myList.listIterator(4).toList()

which yields {"e", "f", "g"} as a new list.

Y


On Dec 9, 10:26 am, Carson Gross <carsongr...@gmail.com> wrote:
> On List, the subList() method returns a "view" of the larger list, so
> modifications will percolate through to it:
>
> http://download.oracle.com/javase/1.4.2/docs/api/java/util/List.html#...)
>
>   var myList = {"a", "b", "c}
>   myList.subList( 0, 2 ).clear()
>   print( myList ) // prints ["c"]
>
> Cheers,
> Carson
>
> On Thu, Dec 9, 2010 at 6:47 AM, Brandon Gresham <bran...@thegreshams.net>wrote:
>
>
>
>
>
>
>
> > You could use java's ArrayList...
>
> >        ArrayList.removeRange( 0, N )
>
> > For arrays, you could massage to an ArrayList first...
>
> >        var arrayList = Arrays.asList( myArray ) as ArrayList
>
> > -----Original Message-----
> > From: gosu...@googlegroups.com [mailto:gosu...@googlegroups.com] On
> > Behalf Of dw
> > Sent: Thursday, December 09, 2010 12:51 AM
> > To: gosu-lang
> > Subject: [gosu-lang:187] Truncate an array / list?
>
> > Silly question, but I'm wondering what the best way is to remove the
> > first N elements of an array or list.  I didn't see anything obvious
> > in the docs.
>
> > Cheers,
>
> > David
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "gosu-lang" group.
> > To post to this group, send email to gosu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > gosu-lang+...@googlegroups.com<gosu-lang%2Bunsubscribe@googlegroups .com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/gosu-lang?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "gosu-lang" group.
> > To post to this group, send email to gosu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > gosu-lang+...@googlegroups.com<gosu-lang%2Bunsubscribe@googlegroups .com>
> > .

Brandon Gresham

unread,
Dec 9, 2010, 8:27:42 PM12/9/10
to gosu...@googlegroups.com
I guess the point being driven home is that the kewlness of Gosu allows you to just use a standard-java approach whenever Gosu doesn't provide the functionality.

;)




To unsubscribe from this group, send email to gosu-lang+...@googlegroups.com.

Carson Gross

unread,
Dec 10, 2010, 11:12:44 AM12/10/10
to gosu...@googlegroups.com
Exactly.  A good algorithm to use when asking "How do I do this in Gosu?" is:

  How do I do this in Java?  Is it insane?  

  If yes, Gosu probably offers a better alternative (e.g. File#read() )

  If no, the Java way is probably the right way

;)

Cheers,
Carson

Brandon Gresham

unread,
Dec 10, 2010, 11:54:25 AM12/10/10
to gosu...@googlegroups.com

Yeah, File#read() is quite awesome.  Even the gosu-anti-fanboys where I work (because they are just *-anti-fanboys, nothing specifically against Gosu) were very pleased to see that feature.

Reply all
Reply to author
Forward
0 new messages