A Pipe to the Future

Paolo Bonzini bonzini at gnu.org
Wed Sep 19 12:45:15 UTC 2007


> LazyList enummerateFrom: 0 with: [:e| e + 2]    "infinite list of even
> numbers, starting at 0"
> 
> and defines many functional maps, transforms and reductions with a
> Smalltalk naming convention, e.g.:

Smalltalk already has a powerful abstraction for lazy lists, i.e. 
streams; you can add #select:/#reject:/#collect:/#inject:into: to 
streams.  They would be lazy operations, unlike the ones on collections, 
since Streams are possibly infinite.

You can also have generators as in Python (it is relatively easy to 
implement them on top of continuations) to create a "pluggable" stream.

> SomeClass>>sieve: aLazyList
>   ^ LazyList
>       cons: aLazyList first
>       with: (LazyList delay:
>          [ SomeClass sieve: aLazyList allButFirst
>              select [:e| e \\ aLazyList first ~= 0 ] ])

Cool stuff.

With stream-like generators, this would look like:

odds := Generator inject: 3 into: [ :prev | prev + 2 ].
primes := Generator inject: 2 into: [ :prev "unused" |
    | prime |
    prime := odds next.
    odds := odds select: [ :each | each \\ prime ~= 0 ].
    prime ].

After staring at the two for a while, they look pretty similar actually 
(aLazyList => odd, cons:with: => inject:into:, first => next which also 
removes the first so my version does not need allButFirst).

Paolo



More information about the Squeak-dev mailing list