Code Block / Message to List
When I'm coding my apps and I'm going through lists ( very often ). I find it tiring to create an iterator or for loop for every little thing. I'd love to send "Code Blocks" to a List, a la Ruby / Smalltalk; and every element in that list has the code block executed on it. Is this in some standard library somewhere? I can't imagine someone hasn't done this.
So the code infig.1 would output:
List BEFORE: [Something old, Something blue, Something More...]
List AFTER: [Pptuiiiii!!!, Pptuiiiii!!!, Pptuiiiii!!!]
publicstaticvoid main(String args[]){
IRList irlist =new IRArrayList();
irlist.add("Something old" );
irlist.add("Something blue" );
irlist.add("Something More..." );
System.out.println("List BEFORE: "+ irlist );
irlist.acceptBlock(
new ICodeBlock(){
public Object message( Object listElement ){
listElement ="Pptuiiiii!!!";
System.out.println("CodeBlock Element: "+ listElement );
return listElement;
}
}
);
System.out.println("List AFTER: "+ irlist );
}
fig. 1
Tim
[1722 byte] By [
timmeeeeea] at [2007-10-2 20:02:36]

Can't see it being terribly useful myself. Do you have a real-world application for that to illuminate the requirement?
It wouldn't be terribly hard to create something like that if there were a real need for it, but if there isn't then there's probably not an implementation like that.
Remember: Java is not language X. If you need language X features it may be better to use language X.
Here's an example of how it might be implemented if you really need it. I still think it's pointless though.
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ItemSwapper {
public static void main(String... args) {
List<String> list = new ArrayList<String>();
list.add("Something old");
list.add("Something blue");
list.add("Something More...");
System.out.println("List BEFORE: " + list);
accept(list, new Accept<String>() {
public String swap(String listElement) {
listElement = "Pptuiiiii!!!";
System.out.println("CodeBlock Element: " + listElement);
return listElement;
}
});
System.out.println("List AFTER: " + list);
}
public static <T> void accept(List<T> list, Accept<T> accept) {
ListIterator<T> it = list.listIterator();
while (it.hasNext()) {
it.set(accept.swap(it.next()));
}
}
public interface Accept<T> {
public T swap(T item);
}
}
Thanks for the reply. I can see how simple it is to implement, which is why I figured there must be a widely implemented solution somewhere. I spent about 2 years away from Java development - in Ruby and Javascript. The idea here is to add methods dynamically (Javascipt) and send code snippets to a list to be executed ( Ruby - obviously ). This is why I crave some of those features in Java.
I hear you about using a language for the task. But in some cases, you are required to use Java for example. And in this case, the idea is not to have static 'accept' method. Instead, I want to send code bits to the list itself, to be executed on all the elements. Call me lazy, or just preferring a particular approach. I just get tired of opeing Iterators when I just want to apply some simple code in a loop. It's less utility than semantic candy... which I think counts for just as much.
/tim
btw - I know how to do this. I want to know if there's a common package people are using.
You didn't really answer my question about the real-world scenarios where this would actually be useful. Without that I don't buy any of your arguments in its favour.
The code I posted was a reasonable implementation - but the following code:
ListIterator<String> it = list.listIterator();
while (it.hasNext()) {
String item = it.next();
item = "Pptuiiiii!!!";
System.out.println("CodeBlock Element: " + item);
it.set(item);
}
Achieves exactly the same end result in exactly the same number of lines of code (assuming a pre-existing accept method).
Only if you bolt it onto the List class as you suggest does it save any typing, but "saving typing" isn't going to buy you much credibility when weighde against cluttering up the List API.
And if you just dislike iterators, all I can say is "get over it".
I'm also skeptical that what your suggesting offers any advantages over the Command pattern with a MacroCommand to deal with the iteration and ConcreteCommands to do the specifics of what you need against each list item. The Command Pattern also has a clear advantage because you could Implement different iteration semmantics in different MacroCommands i.e. Ordered or Unordered Iteration.
Dave has a point, there! why shoehorn this into Java when ruby, or the mysterious Language 'X' already does it?
Dave, what dialect of Language X are you using? I've tried OpenX, which I just made up, and it's not bad but not a patch on JX, naturally. don't even get me started on X# though - a poor imitation
remember, any sufficiently complicated program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp
dcminter, you're absolutely correct and I agree with you 100%. And again, my point is that it's less utility than semantic candy (which I think counts for just as much). The real-world scenario is any list over which you want to iterate, execute a small block of code, without a 'break' or 'continue'. So the while loop still a better solution for many cases. But just like an overloaded method, it would be nice to have a quick and dirty way to send code to each element in a list. But it's not a point that I'll harp on - it would just be nice. So I'll probably stick to Ruby in this regard.
list.sendMessage( new ICodeBlock{
public Object message(Object msg) {
return ((String)msg).replace( "oldsnippet", "newsnippet" );
} } );
fig. 1
listiterator li = list.listIterator();
while( li.hasNext() ) {
Object eachElement = li.next();
return ((String)msg).replace( "oldsnippet", "newsnippet" );
}
fig. 2