Hi Slava, Hi Factor list,

I'm sure this must have been discussed before, but I was wondering if 
the sequence combinators would be better served by an underlying 
iterator protocol in addition to the current random access one. I can 
see a couple of advantages of this:

- sequence combinators could be used with streams
- sequence combinators could be used with lazy lists
- outsources iteration to sequence classes, affording opportunites for 
optimization

(By the latter I mean that e.g. a fixnum-length container could optimize 
to fixnum+fast)

I had a quick go at an iterator protocol by way of proof-of-concept. 
(old tuple notation I'm afraid). This protocol is more like python's 
than STL or java. The 'each' combinator was pretty easy:

GENERIC: next ( iterator -- end? )
GENERIC: elem ( iterator -- element )
GENERIC: iter ( seq -- iterator )

USING: math.private ;

TUPLE: fixnum-iterator num end ;
C: <fixnum-iterator> fixnum-iterator

M: fixnum iter ( seq -- iterator ) 0 swap <fixnum-iterator> ;
M: fixnum-iterator elem ( iterator -- element ) fixnum-iterator-num ;

M: fixnum-iterator next ( iterator -- end? )
    dup fixnum-iterator-num 1 fixnum+fast
    dup pick fixnum-iterator-end fixnum<
    [ swap set-fixnum-iterator-num t ] [ 2drop f ] if ;


: each-iter-loop ( iter quot -- )
   2dup [ elem ] dip call
   over next [ each-iter-loop ] [ 2drop ] if ; inline

: each-iter ( seq quot -- )
   [ iter ] dip each-iter-loop ; inline


( scratchpad ) 3 [ . ] each-iter
0
1
2

What do you think?

Cheers,

Phil



-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to