#firstOrLess: and #lastOrLess: ?

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

#firstOrLess: and #lastOrLess: ?

marcel.taeumel
Hi --

Is there a better way to get a limited number of elements from a collection but accept less if the collection is not big enough?

someObjects size >= 5
  ifTrue: [someObjects first: 5]
  ifFalse: [someObjects].

Could we add a #firstOrLess:? Similar thoughts on #firstOrNil, #lastOrNil, and #lastOrLess:.

Best,
Marcel
Reply | Threaded
Open this post in threaded view
|

re: #firstOrLess: and #lastOrLess: ?

ccrraaiigg

Hi Marcel--

> Is there a better way to get a limited number of elements from a
> collection but accept less if the collection is not big enough?

     Traditionally, that would be Stream>>nextAvailable:.


-C

--
Craig Latta
netjam.org
+31   6 2757 7177 (SMS ok)
+ 1 415  287 3547 (no SMS)


Reply | Threaded
Open this post in threaded view
|

Re: #firstOrLess: and #lastOrLess: ?

Karl Ramberg
In reply to this post by marcel.taeumel
The name firstOrLess: is a little confusing.
Maybe something like collectMax: 


Karl

On Thu, Sep 3, 2015 at 1:30 PM, marcel.taeumel <[hidden email]> wrote:
Hi --

Is there a better way to get a limited number of elements from a collection
but accept less if the collection is not big enough?

someObjects size >= 5
  ifTrue: [someObjects first: 5]
  ifFalse: [someObjects].

Could we add a #firstOrLess:? Similar thoughts on #firstOrNil, #lastOrNil,
and #lastOrLess:.

Best,
Marcel



--
View this message in context: http://forum.world.st/firstOrLess-and-lastOrLess-tp4847874.html
Sent from the Squeak - Dev mailing list archive at Nabble.com.




Reply | Threaded
Open this post in threaded view
|

Re: #firstOrLess: and #lastOrLess: ?

marcel.taeumel
It's no "collect" but a "first: n", which returns the first n elements from a collection. So #nextAvailable: reads nice but this is stream semantics. There is no #next for collections.
Reply | Threaded
Open this post in threaded view
|

re: #firstOrLess: and #lastOrLess: ?

ccrraaiigg

> ...#nextAvailable: reads nice but this is stream semantics. There is
> no #next for collections.

     That was a suggestion that you should be using a stream. :)


-C

--
Craig Latta
netjam.org
+31   6 2757 7177 (SMS ok)
+ 1 415  287 3547 (no SMS)


Reply | Threaded
Open this post in threaded view
|

Re: #firstOrLess: and #lastOrLess: ?

Chris Muller-3
In reply to this post by marcel.taeumel
If you *DID* implement a new method, I'd call it, "upToFirst:".

  someObjects upToFirst: 5

However, I don't think its worth expanding the API; I would just do

   someObject first: (5 min: someObject size)

It's not that hard to read.

On Thu, Sep 3, 2015 at 9:32 AM, marcel.taeumel <[hidden email]> wrote:

> It's no "collect" but a "first: n", which returns the first n elements from a
> collection. So #nextAvailable: reads nice but this is stream semantics.
> There is no #next for collections.
>
>
>
> --
> View this message in context: http://forum.world.st/firstOrLess-and-lastOrLess-tp4847874p4847907.html
> Sent from the Squeak - Dev mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: #firstOrLess: and #lastOrLess: ?

Eliot Miranda-2


Sent from my iPhone

> On Sep 3, 2015, at 7:40 AM, Chris Muller <[hidden email]> wrote:
>
> If you *DID* implement a new method, I'd call it, "upToFirst:".
>
>  someObjects upToFirst: 5

I find this slightly ambiguous with upTo: and upThrough:.  Why not atMostFirst: and atMostLast:  ?

>
> However, I don't think its worth expanding the API; I would just do
>
>   someObject first: (5 min: someObject size)
>
> It's not that hard to read.
>
>> On Thu, Sep 3, 2015 at 9:32 AM, marcel.taeumel <[hidden email]> wrote:
>> It's no "collect" but a "first: n", which returns the first n elements from a
>> collection. So #nextAvailable: reads nice but this is stream semantics.
>> There is no #next for collections.
>>
>>
>>
>> --
>> View this message in context: http://forum.world.st/firstOrLess-and-lastOrLess-tp4847874p4847907.html
>> Sent from the Squeak - Dev mailing list archive at Nabble.com.
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: #firstOrLess: and #lastOrLess: ?

Hannes Hirzel
On 9/3/15, Eliot Miranda <[hidden email]> wrote:

>
>
> Sent from my iPhone
>
>> On Sep 3, 2015, at 7:40 AM, Chris Muller <[hidden email]> wrote:
>>
>> If you *DID* implement a new method, I'd call it, "upToFirst:".
>>
>>  someObjects upToFirst: 5
>
> I find this slightly ambiguous with upTo: and upThrough:.  Why not
> atMostFirst: and atMostLast:  ?

+1

>
>>
>> However, I don't think its worth expanding the API; I would just do
>>
>>   someObject first: (5 min: someObject size)
>>
>> It's not that hard to read.
>>
>>> On Thu, Sep 3, 2015 at 9:32 AM, marcel.taeumel <[hidden email]>
>>> wrote:
>>> It's no "collect" but a "first: n", which returns the first n elements
>>> from a
>>> collection. So #nextAvailable: reads nice but this is stream semantics.
>>> There is no #next for collections.
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://forum.world.st/firstOrLess-and-lastOrLess-tp4847874p4847907.html
>>> Sent from the Squeak - Dev mailing list archive at Nabble.com.
>>>
>>
>
>