Fold selector fails on streams

Project:GNU Smalltalk
Component:Base classes

Excuse me for my english.
Streams are Iterable. Class Iterable defines a method fold, which must be sent to any Stream. But in the current implementation method fold can be sent only to instances of PositionableStream, because method fold is based on isEmpty selector. For example, Generator does not provide isEmpty selector:

   st> x:=Generator on: [:g| g yield: 1. g yield: 3 ]
   a Generator
   st> x fold: [:a :b| a+b]
   Object: Generator new "<0x403bc180>" error: did not understand #isEmpty

My solution is:

  fold: aBlock [
     |marker acc|
     acc:=marker:=Object new.
     self do: [:i| acc:=acc==marker ifTrue: [i]
                                    ifFalse: [aBlock value: acc value: i] ].
     acc==marker ifTrue: [ SystemExceptions.EmptyCollection signalOn: self ]
                 ifFalse: [^acc] ]


#1 submitted by Paolo Bonzini on Mon, 10/08/2012 - 15:54
Status:active» fixed

Applied, thanks.

User login