In the Design Pattern iterator there are some nice examples about iterators.

Stef
Le 17/12/14 08:49, Clément Bera a écrit :


2014-12-17 7:42 GMT+01:00 Damien Cassou <damien.cas...@gmail.com <mailto:damien.cas...@gmail.com>>:


    Le 16 déc. 2014 22:48, "dboeren" <boer...@gmail.com
    <mailto:boer...@gmail.com>> a écrit :.
    > Off the top of my head, I would be interested in support for
    "slices"
    > meaning taking being able to treat a subset of a larger
    OrderedCollection as
    > its own OrderedCollection rather than using copyFrom:to: to create a
    > duplicate object to work with.  It seems to me that this would
    be a more
    > efficient method and should not be difficult to implement I think.

    Here is an email I sent about that :

    I see you are working on new collections for Pharo. That's great news,
    I'm convinced we can do much better (even if what we already have is
    already much better than what can be found in most other languages).
    One thing you could work on is the notion of iterator. There are
    plenty of ways to iterate over a collection:

    1/ from the first to the last item
    2/ from the last to the first
    3/ from the 2nd item to the 5th
    4/ only even items (2, 4, 6, ...)
    5/ ...

    Currently, we have to create new collections to iterate in strange
    ways. For example, to iterate from the last to the first item, we
    create a new collection by sending #reversed to the original
    collection. I think that is bad because it creates a copy and that
    should not be necessary.

I don't understand that example. #reverseDo: do not create a copy and do not send reversed. To iterate over items from last to first I always do, as recommended in Smalltalk best practice pattern:

mySequenceableCollection reverseDo: [:item | .. ].

#reverseDo: is implemented on SequenceableCollection but iterating from last to first item does not make sense in the case of non sequenceable collection.

    What we miss is the notion of iterator. An
    iterator is an object that is specialized in iterating over any kind
    of collection and has methods like #select:, #reject:, #allSatisfy:.
    >From a collection, you can access different kinds of iterators
    (reusing the same ordering as before):

    1/ aCollection iterator
    2/ aCollection reverseIterator
    3/ aCollection from: 2 to: 5
    4/ aCollection iterator select: [ :each | each isEven ] --> that
    returns an iterator as well, so you can call #select:/#reject:
    multiple times without creating any intermediate collection
    5/ ...

That makes sense and would be lovely. What is the syntax for multiple operations ? Have you already think and find something nice about it ?

aCollection iterator
    select: [ :each | each isOdd ];
    reject: [ :each | each is isPrime ];
    iterate

???

Of course there are backward compatibility issue which needs to be very well adressed here or one would crash every single framework in the system.

    Lukas Renggli implemented a prototype that looks really nice but is
    unfinished:
    http://source.lukas-renggli.ch/unsorted/Container-lr.7.mcz.

    I think the notion of Sequences in Clojure is very related as well.
    Sequence functions are very generic and can be applied to any kind of
    collection. http://clojure.org/sequences


Reply via email to