Le 22/03/2015 18:57, stepharo a écrit :
may be a first step would be to have findFirst: aBlock startingAt: based
on findFirst:


findFirst: aBlock
     "Return the index of my first element for which aBlock evaluates as
true."

     | index |
     index := 0.
     [(index := index + 1) <= self size] whileTrue:
         [(aBlock value: (self at: index)) ifTrue: [^index]].
     ^ 0



Hi

I need a collection that does the following:

col := #('a12' 'b12' 'a13' 'a14' 'c23' 'a16') asMyNewCollection.
col findFirst: [:each | each first = $a]
    > a12
col findFirst: [:each | each first = $a]
    > a13

Do you know a collection that would work?
So I have the impression that such behavoir could be defined with a
kind of methods wrapper.

Stef






May be this "memory" could be implemented in another object (and/or Traits?) , allowing several threads to traverse the collection, and add extensibility to the collection framework.

The general idea, inspired from dotnet+smalltalk
(I don't know if this fit well with smalltalk patterns, may be total junk and not doable ? -btw it's just a proposition)

Collection>> asQueryable
"=> a Queryable Object that would implement first, next, based on a predicate."
        ^Queryable on: self readStream.

Queryable class >> on: aStream
        ^self new on: aStream

Queryable>> where: aPredicateBlock
        self predicate: aPredicateBlock

Queryable>> first
        self stream reset .
        ^self next.
Queryable>>next
        [self stream atEnd not
        and: [ (self predicate value: (current := self stream next)))] 
whileFalse.
        ^ self stream atEnd ifTrue: [nil]
                ifFalse: [self current].
(...)

col := #('a12' 'b12' 'a13' 'a14' 'c23' 'a16') asQueryable where: [:each | each first = $a].
col first. -> 'a12'
col next. -> 'a13'

Those dotnet linq patterns are cool, on a dynamic langage like smalltalk with
tools like GTInspector + roassal and Moose, wouldn't it be ultra cool ?
 :)

--
Regards,

Alain


Reply via email to