get the first n elements of an Collection

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

get the first n elements of an Collection

sergio_101
hey all..

this is more of a style question than anything else..

what is the most 'smalltalk' way to grab the first n elements of an
ordered collection?

using an interval? using an iterator?

thanks!

--
----
peace,
sergio
photographer, journalist, visionary

http://www.ThoseOptimizeGuys.com
http://www.CodingForHire.com
http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

Reply | Threaded
Open this post in threaded view
|

Re: get the first n elements of an Collection

Mariano Martinez Peck
#first:

On Thu, Sep 27, 2012 at 5:39 PM, sergio_101 <[hidden email]> wrote:
hey all..

this is more of a style question than anything else..

what is the most 'smalltalk' way to grab the first n elements of an
ordered collection?

using an interval? using an iterator?

thanks!

--
----
peace,
sergio
photographer, journalist, visionary

http://www.ThoseOptimizeGuys.com
http://www.CodingForHire.com
http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101




--
Mariano
http://marianopeck.wordpress.com

Reply | Threaded
Open this post in threaded view
|

Re: get the first n elements of an Collection

Bernat Romagosa
In reply to this post by sergio_101
Hi Sergio,

You can do aCollection first: n

Cheers

2012/9/27 sergio_101 <[hidden email]>
hey all..

this is more of a style question than anything else..

what is the most 'smalltalk' way to grab the first n elements of an
ordered collection?

using an interval? using an iterator?

thanks!

--
----
peace,
sergio
photographer, journalist, visionary

http://www.ThoseOptimizeGuys.com
http://www.CodingForHire.com
http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101




--
Bernat Romagosa.
Reply | Threaded
Open this post in threaded view
|

Re: get the first n elements of an Collection

Guillaume Larcheveque
In reply to this post by sergio_101
myCollection first: n

2012/9/27 sergio_101 <[hidden email]>
hey all..

this is more of a style question than anything else..

what is the most 'smalltalk' way to grab the first n elements of an
ordered collection?

using an interval? using an iterator?

thanks!

--
----
peace,
sergio
photographer, journalist, visionary

http://www.ThoseOptimizeGuys.com
http://www.CodingForHire.com
http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101




--
Guillaume Larcheveque


Reply | Threaded
Open this post in threaded view
|

Re: get the first n elements of an Collection

sergio_101
In reply to this post by Bernat Romagosa
thanks, guys..

for whatever reason, i couldn't seem to find that in
SequenceableCollection. but after your posts, i did a search, and it
was there.. my bad..

--
----
peace,
sergio
photographer, journalist, visionary

http://www.ThoseOptimizeGuys.com
http://www.CodingForHire.com
http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

Reply | Threaded
Open this post in threaded view
|

Re: get the first n elements of an Collection

Damien Cassou
On Thu, Sep 27, 2012 at 5:49 PM, sergio_101 <[hidden email]> wrote:
> for whatever reason, i couldn't seem to find that in
> SequenceableCollection. but after your posts, i did a search, and it
> was there.. my bad..

for these kinds of queries, never forget to check streams:

aCollection asStream next: 10

In this case you can use #first: as others stated, but in many other
cases streams will be the only way to solve your problem.

--
Damien Cassou
http://damiencassou.seasidehosting.st

"Lambdas are relegated to relative obscurity until Java makes them
popular by not having them." James Iry

Reply | Threaded
Open this post in threaded view
|

Re: get the first n elements of an Collection

Sven Van Caekenberghe-2

On 29 Sep 2012, at 12:22, Damien Cassou <[hidden email]> wrote:

> for these kinds of queries, never forget to check streams:
>
> aCollection asStream next: 10
>
> In this case you can use #first: as others stated, but in many other
> cases streams will be the only way to solve your problem.

Yes, that is a good idea as well, and not even as inefficient as it seams.

But the selector is #readStream (or #writeStream).

Actually, using ReadStream class>>#on:from:to: can solve lots of problems efficiently as well.

--
Sven Van Caekenberghe
http://stfx.eu
Smalltalk is the Red Pill