[erlang-questions] eep-0012 (Extensions to comprehensions)

Andras Georgy Bekes bekesa@REDACTED
Wed Aug 6 17:19:55 CEST 2008


Hi,

I like the idea of extending comprehensions to tuples, twisting language 
constructs to being more generic is always a good idea (to think 
about :-)

What I miss is the ability to extend comprehensions by user defined 
generators, i.e. to be able to iterate over any data structure with a 
generator.

My propose is to introduce a fourth generator:
1, [<-] is for iterating over lists
2, {<-} is for iterating over tuples
3, <<<->> is for iterating over binaries
and
4, (<-) for iterating over anything else.

In the generator "X (<-) Expr", Expr should evaluate to a function that 
returns either an empty list or the next element and the continuation 
fun.

There are many possible types, just to mention a few:

----------------
Using tuples:

Generator = fun() -> Result
Result = [] | {term(),Generator}

----------------
List-like:

Generator = fun() -> Result
Result = [] | [term()|Generator]

----------------
QLC's TraverseFun type simplified:

Generator = fun() -> Objects
Objects = [] | [term() | ObjectList]
ObjectList =  Objects | TraverseFun

--------------------
And many others. (I prefer the last.)

The standard data types (arrays, sets, dicts, trees, etc) should provide 
their own iterator functions, and defining custom iterators is very 
easy.

Example:
--------------------
term_reader()->
    case io:read("Type any term or 'quit' >") of
	{ok,quit} -> [];
	{ok,Term} -> [Term|fun term_reader/0]
    end.
--------------------

Real nice would be if we'd extend not only generators, but the generated 
data as well. So we could, besides lists, tuples and binaries, generate 
any data structure by feeding the generated elements to a similar fun.
However, I can't find any good syntax for that.

How do you like the idea?

	Georgy

PS: I know, I know, QLC does the same and much more, but QLC is too 
heavyweight and you rarely use it because of that.



More information about the erlang-questions mailing list