Bad reimplementation of Collection>>collect:thenDo: in Pharo 3.0

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

Bad reimplementation of Collection>>collect:thenDo: in Pharo 3.0

Tomas Kukol
This post was updated on .
Hi list,

I have found a bad reimplementation of Collection>>collect:thenDo: in Pharo 3.0.

1) New implemetation from 3/22/2013 22:18 is BAD:

Collection>>collect: collectBlock thenDo: doBlock
        "Utility method to improve readability."
       
        ^ self do: [ :each|
                doBlock value: (collectBlock value: each)]

2) GOOD (but not optimal) code is this:

Collection>>collect: collectBlock thenDo: doBlock
        "Utility method to improve readability."
        ^ (self collect: collectBlock) do: doBlock

The #collect: message always produce a new collection, the BAD code doesn't produce a new collection. Collected items are thrown away.

Consider this code:

1) BAD

{ 1@1. 2@2. 3@3 } collect: [ :each | each * 2 ] thenDo: [ :each | each setX: each x - 1 setY: each y + 1 ] -> {(1@1). (2@2). (3@3)} "<- this is BAD result"

2) GOOD (old implementation)

({ 1@1. 2@2. 3@3 } collect: [ :each | each * 2 ]) do: [ :each | each setX: each x - 1 setY: each y + 1 ] -> {(1@3). (3@5). (5@7)} "<- this is GOOD result"

Best regards,
Tomas Kukol