4 Apr 2008 tampe   » (Apprentice)

A generator of life and you can play god

I've been reading about iterators previously and have gone through the main construction in the python and java environments

What's especially cool is the python iterator generator stuff with the yield statement. I'm now working on a functional version of it (state is in input and output) that uses tail recursion in almost all it's constructs e.g. a good compiler should be able to optimize the generator to yield high enough performance

What I lack is a more diversified iterator generator framework, it looks from my small survey that the art of constructing generators from generators is lacking. Note the zip iterator generator in python though

First of I will take notice on the trend on separating data from logic and make my loop construct as follows.

(for Var in (Generator Data) Reducer Code)
Var is the iterator values used in the code. The Generator implements a typical iterator construct in a functional manner:
define Generator  
    init Data  -> (return a initiated state)
    curr State -> (return the value of the state)
    next State -> (return the next state)

The reducer initiates to a value, and from the previous results and the value of the code outputs a new result. A finialization of the result may be needed as well leaving.

define Reducer
    init Generator Data -> (initialized result)
    redu OldRes Value   -> (combines oldres and value)
    fini Res    Gen     -> (outputs the final value)
This is quite general but may not of cause be the final version in this exercise. Note that when doing this construct we are very functional and don't consider the chaging the Data during iteration hence this discussion is about transformation of the data to new datastructures that means expensive but safe operations. Now I have identified three transformation operations I would be able to do on generators, namly o,x,zip. The zip operations takes a set of generators and iterates them in parallel until any of them finalizes. The x operation behaves like a generation of a iteration over a matrix and the o operation behaves like a tree iteration generator with a fixed depth.

Also A iteration notion should be able to do what parser do. Code is a tree with a syntax and you should be able to do an iteration of the code tree producing for example a simplified tree representing the code or whatever. My view is that if standard environment of doing iterators does not do this there is more work to do. It's not that much code to do implement that really (I speculate that this actually can be not too expensive if you do a good parser or "just in time" parser handling tail recursion, inlining and type theory to optimize, which has to be implemented anyway, right!)

And yes, the expression

 
(loop for (X in (list L ))
          (Y in (array A))
          (if X < Y
              (for Z in range(X,Y) generate))))
should yield a generator

I think that this effort will lead to an excellent discussion about generator programming and therefore my intention is in the end to wrap up my effort in a tutorial kind of document for hackers to enjoy over a glas of wine perhaps ;-)

As we say in Sweden to the polar bears, skål på er.

Latest blog entries     Older blog entries

New Advogato Features

FOAF updates: Trust rankings are now exported, making the data available to other users and websites. An external FOAF URI has been added, allowing users to link to an additional FOAF file.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!