Why do #select:thenXxx methods not return their result

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

Why do #select:thenXxx methods not return their result

Tim Mackinnon
Hi - are the methods like #select:thenCollect: frowned upon?

They seem quite readable , however in using them I’ve noticed that unlike the core methods they done return the result of evaluation (they are missing a ^). This is a shame, but possibly an oversight?

Tim

Sent from my iPhone

Reply | Threaded
Open this post in threaded view
|

Re: Why do #select:thenXxx methods not return their result

Peter Uhnak
How do you mean?

(1 to: 10) select: #odd thenCollect: [ :x | x ** 2 ] "-> #(1 9 25 49 81)"

It wouldn't make sense otherwise to have the collect method if it wouldn't return anything.

Peter

On Thu, Jun 7, 2018 at 2:20 PM, Tim Mackinnon <[hidden email]> wrote:
Hi - are the methods like #select:thenCollect: frowned upon?

They seem quite readable , however in using them I’ve noticed that unlike the core methods they done return the result of evaluation (they are missing a ^). This is a shame, but possibly an oversight?

Tim

Sent from my iPhone


Reply | Threaded
Open this post in threaded view
|

Re: Why do #select:thenXxx methods not return their result

Tim Mackinnon
You are correct Peter - I jumped the gun, as it was #select:thenDo: where I hit the problem - and yes it doesn’t make sense to answer a result from Do.

I do kind of miss the concept of: #select:thenDo:ifNone: as well as #select:thenCollect:ifNone: but some bracketing sorts it out, and I guess where do you stop.

Tim

On 7 Jun 2018, at 13:25, Peter Uhnák <[hidden email]> wrote:

How do you mean?

(1 to: 10) select: #odd thenCollect: [ :x | x ** 2 ] "-> #(1 9 25 49 81)"

It wouldn't make sense otherwise to have the collect method if it wouldn't return anything.

Peter

On Thu, Jun 7, 2018 at 2:20 PM, Tim Mackinnon <[hidden email]> wrote:
Hi - are the methods like #select:thenCollect: frowned upon?

They seem quite readable , however in using them I’ve noticed that unlike the core methods they done return the result of evaluation (they are missing a ^). This is a shame, but possibly an oversight?

Tim

Sent from my iPhone



Reply | Threaded
Open this post in threaded view
|

Re: Why do #select:thenXxx methods not return their result

monty-3
In reply to this post by Tim Mackinnon
Not just more readable. They can also be more efficient. Look at #select:thenCollect: in OrderedCollection:
select: selectBlock thenCollect: collectBlock
    " Optimized version Collection>>#select:thenCollect: "

        | newCollection element |

    newCollection := self copyEmpty.

    firstIndex to: lastIndex do: [ :index |
                element := array at: index.
                (selectBlock value: element)
                        ifTrue: [ newCollection addLast: (collectBlock value: element) ]].

    ^ newCollection

It only uses one temp collection, where a #select: followed by a separate #collect: would need two.

___
montyos.wordpress.com


> Sent: Thursday, June 07, 2018 at 8:20 AM
> From: "Tim Mackinnon" <[hidden email]>
> To: "Pharo Users Newsgroup" <[hidden email]>
> Subject: [Pharo-users] Why do #select:thenXxx methods not return their result
>
> Hi - are the methods like #select:thenCollect: frowned upon?
>
> They seem quite readable , however in using them I’ve noticed that unlike the core methods they done return the result of evaluation (they are missing a ^). This is a shame, but possibly an oversight?
>
> Tim
>
> Sent from my iPhone
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Why do #select:thenXxx methods not return their result

Peter Uhnak
It only uses one temp collection, where a #select: followed by a separate #collect: would need two.


Yeah, be careful with that, because it changes the semantics of the operation. (magine what happens when `thenDo:` operates on the original collection and there's no intermediate collection. (I ran into this issue many times.)

Peter