January 19, 2007


This week’s Xing (I nearly called it bugflug) will probably be trying to fix stuff on the compsoc website. This should be nice and accessible stuff that has obvious benefit to people. But, I am still interesting in running some more ‘exotic’ projects, so I thought I would air some thoughts on the proposed collections operations to be implemented in Javac, which I hope to work on again with Lamby and maybe more people if they are interested.

The essential idea of the for .. do loop is to be a variant of the map higher order function that can be found in both functional languages such as Ocaml and dynamic languages like Python. So the basic idea that was proposed last week was:

for do

Where the expr evaluates to a collections object. For example the following conversion would occur in Javac:

for this.list do m;


List newList = new Vector ;
for(ListElementType element:this.list) {
this.list = newList;

This conversion happens with the safety condition that the method m takes one parameter of type ‘ListElementType’ and its return type is of the same type, where ListElementType is the type of element’s of this.list. Some implementation effort has gone on with this idea, and I think it would be possible to finish it at the next LAN with some concerted effort and thought.

I was thinking the other day of some extensions to this basic syntax, for example by adding multiple parameters, so that:

for this.list1 this.list2 do m;


if(this.list1.size() == this.list2.size()) {
List newList = new Vector ;
for(int i = 0;i newList.add(m(this.list1.get(i),this.list2.get(i)));
this.list = newList;

Obviously this would be implemented for n number expressions, rather than just two. Similar safety conditions apply as the first example. The primary issue that I can think of is that one of the safety conditions has to be checked at runtime, which doesn’t really fit into Java’s paradigm.

There would also be the potential for adding non-list parameters to such a construct, that would be passed to every method call. For example:

for this.list this.variable do m


List newList = new Vector ;
for(ListElementType element:this.list) {
this.list = newList;

Such things obviously add more complexity to java, but I think for very commonly used syntactic forms such as this, it might be a good idea to have such a construct.

- 3 comments by 0 or more people Not publicly viewable

  1. Daniel Watkins

    Would it be of value to be able to say:
    List foo = for bar do spam;
    and have the resulting list of ‘for bad do spam’ put into another list?

    I think this is a fairly common requirement and people will have to copy their list before they use the for..do, which seems wasted. Ideally you could still do:
    bar = for bar do spam;
    which would have exactly the same effect as what you’ve described.

    If you wanted to be really pro you could make that the default so if the for…do isn’t being assigned anywhere it assumes the ‘bar =’ part.

    19 Jan 2007, 02:26

  2. Neal Gafter

    My approach would be to add closures and extension methods (ala C#), and then write a method that acts as an extension method to List, in which case you would express this as

      list.map({ListElementType element => m(element)});

    By would be, I mean I’m working on it.

    19 Jan 2007, 04:19

  3. Tim

    You are SUCH a geek, Mullet.

    09 Feb 2007, 01:18

Add a comment

You are not allowed to comment on this entry as it has restricted commenting permissions.

January 2007

Mo Tu We Th Fr Sa Su
Dec |  Today  | Feb
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31            

Search this blog



Most recent comments

  • Apart from the plea "can I have your old one?" (the yearning never diminishes) I'd like to leave you… by Sue on this entry
  • Unfortunately I still haven't seen a film for which you have to read up on quantum mechanics as I mi… by Sue on this entry
  • I've never been to watch a film before when it's been recommended that you first read up on quantum … by Sue on this entry
  • Well this is very interesting, i really liked reading this blog, this was very informative and very … by Mio Navman Spirit S300 on this entry
  • I thought it was fascinating. Griffin isn't like any other, media–trained, polished politician, and … by Tim on this entry

Blog archive



Not signed in
Sign in

Powered by BlogBuilder