My Strange Loop submission is the "new To" Loop.  I only need one
ticket.

 The "new To" loop provides a looping alternative that is often
slightly more concise than the for loop.  It allows you to replace:

    for (Object x : new Integer[] {8,6,7,5,3,0,9}) { print("Jenny:" +
x); };

with:

    new To(8,6,7,5,3,0,9) {{ print("Jenny:" + x); }};

Unlike, the standard for loop, the new To loop also accepts IteratorS
and EnumerationS:

    new To(System.getProperties().keys()) {{ print(x); }};

A To is an object that implements several interfaces, so that adapters
aren't required in order use it in a variety of ways.  Because it is a
Collection, it can be executed again later, along with additional
code, using a for loop.

    for (Object x : to) {
        // do additional stuff to x, here
    }

If no additional action is to be taken on the subsequent runs, it can
simply be run as a Runnable, or called as a Callable.  No matter how
the loop is executed again, any registered ObserverS are notified.
Thus, in addition to being nested like for loops, To loops can be
wired together for interleaved execution.

    To tick = new To(1,2,3) {{ print("tick:" + x); }};
    To tock = new To(4,5,6) {{ print("tock:" + x); }};

    tick.addObserver(tock);

    tick.run();

Unfortunately, despite these advantages, the current implementation
has several glaring limitations.

1) Special care must be taken to handle empty loops
2) It only supports To loops that are static classes
3) It has much more overhead and is much slower than an equivalent for
loop

Full source code is available here:
http://dl.dropbox.com/u/8066/To.java

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to