[Pharo-project] Issue 3234 in pharo: fold: reduce: Merge

pharo at googlecode.com pharo at googlecode.com
Thu Nov 11 19:26:35 CET 2010


Status: FixedWaitingToBePharoed
Owner: stephane.ducasse
Labels: Type-Squeak

New issue 3234 by stephane.ducasse: fold: reduce: Merge
http://code.google.com/p/pharo/issues/detail?id=3234

I imagine that reduce: and fold: are not the same in pharo so we should pay  
attention.


Name: Collections-ul.403
Author: ul
Time: 7 November 2010, 1:55:55.898 am
UUID: 7f81b384-8004-cd44-81e0-9b294d999c8a
Ancestors: Collections-ul.402

- merged Collection's #fold: and #reduce: (it's a bit faster than the  
original versions :))


Item was changed:
  ----- Method: Collection>>fold: (in category 'enumerating') -----
  fold: binaryBlock
        "Evaluate the block with the first two elements of the receiver,
         then with the result of the first evaluation and the next element,
         and so on.  Answer the result of the final evaluation. If the  
receiver
         is empty, raise an error. If the receiver has a single element,  
answer
         that element."
        "#('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') fold: [:a :b |  
a, ' ', b]"

+       ^self reduce: binaryBlock!
-       | firstValue nextValue |
-       firstValue := nextValue := Object new. "something that can't be in  
the receiver"
-       self do:
-               [:each |
-               nextValue := firstValue == nextValue
-                                               ifTrue: [each]
-                                               ifFalse: [binaryBlock  
value: nextValue value: each]].
-       ^nextValue == firstValue
-               ifTrue: [self errorEmptyCollection]
-               ifFalse: [nextValue]!

Item was changed:
  ----- Method: Collection>>reduce: (in category 'enumerating') -----
  reduce: binaryBlock
        "Apply the argument, binaryBlock cumulatively to the elements of the  
receiver.
        For sequenceable collections the elements will be used in order, for  
unordered
        collections the order is unspecified."

        | first nextValue |
-       self emptyCheck.
        first := true.
+       self do: [ :each |
+               first
+                       ifTrue: [ nextValue := each. first := false ]
+                       ifFalse: [ nextValue := binaryBlock value:  
nextValue value: each ] ].
+       first ifTrue: [ self errorEmptyCollection ].
-       self do:[:each|
-               first ifTrue:[nextValue := each. first := false]
-                       ifFalse:[nextValue := binaryBlock value: nextValue  
value: each]].
        ^nextValue!






More information about the Pharo-project mailing list