Sponsored Link •
|
Summary
For loop constructs in programming languages are usually syntactic sugar, but what should they map to?
Advertisement
|
In C/C++ a for statement maps from:
for (initialization_statement; boolean_invariant_condition; variant_statement) loop_bodyto more-or-less the following:
{ initialization_statement; while (boolean_invariant_condition) { { loop_body; } variant_statement; } }I wanted do something new with for loops in Heron which leverages generics. Here is the general form:
for iterator_type iterator_name in iterable_expression { loop_body }Some examples:
for int i in range(0, 100) { write_int(i); write_char('\n'); } for char c in "hello" { write_char(c); } for object o in (42, 3.14, 'q') { write_object(o); }
I am happy with this so far, but I want to go further and generalize the notion so that programmers can define new constructs like for-loops. For instance I would like to be able to iterate over the collection in an non-deterministic order. This is important for optimization on concurrent machines.
The best way to generalize this I can think of is to use closures. This way for-loops and programmer defined constructs can map to anonymous functions. The problem right now is trying to figure out an elegant and efficient way to implement closures in HeronFront. In order to implement closures in C++, It seems I have to generate a context data type and pass it to the anonymous function.
Any thoughts?
Have an opinion? Readers have already posted 10 comments about this weblog entry. Why not add yours?
If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.
Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com. |
Sponsored Links
|