How do you change all elements in a Collection at the same time?

L

Lew

Daniel said:
True, but what would be the harm in having an interface:

public interface Closure<R, P> {
R execute(P);
}

This already exists.
and a new language feature:

public <T> void methodWhichTakesClosure(Closure<T, T> closure);
...
methodWhichTakesClosure(closure<Integer, Integer>{return arg+1; });
which would be equivalent to:
methodWhichTakesClosure(new Closure<Integer, Integer>() {
Integer execute(Integer arg) {
return arg + 1;
}
});

This just looks like syntactic sugar, much like the enhanced for-loop. If you
think

closure<Integer, Integer> { return arg+1; }

which to my eye lacks any resemblance to a method signature that can be
enforce, is clearer than

new Closure<Integer, Integer>()
{
Integer execute(Integer arg)
{
return arg + 1;
}
}

than I see why you favor it. To me, the latter form is much more explicit and
plenty easy to understand, and leaves less to guess than the first format.
Anything that makes the intention of the calling code clearer is a Good
Thing(TM) in my opinion, and its [sic] just as type safe.

I agree that clearer is better, but not that your closure form is clearer.
While we're adding keywords and language features, maybe a shortcut to
"Runnable" or "Callable" would be a useful idiom as well.

SwingUtilities.invokeLater(runnable{updateGuiWith(results);});

I have the same objections. I cannot tell how the syntax here tells me what
exactly is happening, whereas the "new Runnable() { public void run(){...} }"
syntax reveals all and hides nothing. To me, the existing Java syntax is
clearer, and therefore by our common principle of "clearer is a Good Thing" we
should stick with what we've got.

- Lew
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Speaking on behalf of the "kiddie script" writers, everything you said
sailed over my head like a satellite. I have no clue what you're
talking about, and I happen to love eval() because it makes me write
one line of code instead of hundreds of lines of unmanageable code
every time. Can't see the entanglement, dude.

eval and closures are really not related - the first is a
library feature - the second is a language feature.

We could add eval to Java. Since Java 1.6 support JavaScript
it has been added to Java.

I think the dynamicly typing features is better to divide
the waters.

A lot of programmers like the old way of declaring something
to have a type and the compiler & runtime to check it.

This paradigm is being challenged by some languages that has become
popular the last 5 years (some of the languages are actually way
older).

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Lew said:
This just looks like syntactic sugar, much like the enhanced for-loop.

Ofcourse.

In the end the entire Java language is syntactic sugar
over the instruction set on the system you run your app on.

The question is: does the code become sufficient easier to
write & read and will it be used frequently enough to warrant
an extension to the language.

It is not free to make he language bigger and more complex.
We know how PL/I and Ada ended up.

I doubth it is worth it for closures.

Arne
 
L

Lasse Reichstein Nielsen

Lew said:
As so many on this thread have pointed out, Java can do what closures
do, you just have to think in terms of object-orientation and
polymorphism instead of interpreted, loosey-goosey self-rewriting code.

Having closures in a language is completely unrelated to being
interpreted, loosely typed, or otherwise "unproffessionel". Take a
language like Haskel, which has a very string, but also powerful, type
system. It's functional though, which is where closures typically
occour.
Many have argued for closures in Java. I am against them for the
moment, since there are already idioms that accomplish the same thing
without the conceptual mindtwist.

For someone used to programming in functional languages, the
conceptual mindtwist comes when you try to express a perfectly simple
anonymous function value as an object of some unnecessary class or
interface. It's just syntactic wrapping necessary to live in a language
where only objects (and primitives) are expressible and denotable objects.

The argument for closures isn't really about functionality, which
is there already, but about syntax. Just as enums were added to provide
simpler syntax for a commonly occuring programming construct, simpler
syntax could be provided for function values.

Simpler syntax, like for enums, provides nothing but readability. It
makes it easier to express the programmers intent and easier to
recognize it for a reader. The more unnecessary syntax is wrapped
around the essential idea, the less accessible it is.

What is easier to read:

myCollection.foreach(new Callable<Integer>() {
void call(Integer v) {
sum += v;
}
});

or

myCollection.foreach(fn(Integer v) { sum += v; });

Once again, that would be polymorphism, possibly with inner or
anonymous classes, combined with generics to provide compile-time
safety.

It's quite possible. It's just so ... wordy.
Java is a professional language, not a script-kiddie hack
language. Things like program invariants, compile-time safety and
maintainability carry weight here.

Which is why generics was a good addition (collections were untyped
before). And why syntactic sugar making often used idioms were made
easier to express: enums, iterations over iterables and arrays,
boxing and unboxing.
I happen to think that easier syntax for delegates, which is essentially
what is needed, would be great. Real closures capturing local variables
might not be necessary, but it would be nice.

/L
 
G

gafter

Lew said:
As so many on this thread have pointed out, Java can do what closures do...

Many have argued for closures in Java. I am against them for the moment, since
there are already idioms that accomplish the same thing without the conceptual
mindtwist...

Actually, you can't do without closures what you can do with them; they
actualy reduce the conceptual mindtwist. Admittedly, you may have to
learn something.

See
http://www.bejug.org/confluenceBeJUG/display/PARLEYS/Closures+for+Java
 
P

pkriens

Right! programming should be hard so only real men can do it! All this
sissy stuff of auto-boxing and foreach loops.

Kind regards,

Peter Kriens
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) schreef:
Eric said:
Paul Hamaker wrote:
As of Java5 :
for ( SomeClass c : somecoll )
{ //do something with (each) c in somecoll, like :
c.change(somearg);
But what if you won't know what "change()" will be? [...]
Then you don't know whether the "change" will preserve
the collection's invariants. Imagine traversing a HashSet
and applying a transformation that changed the hashCodes(),
or a TreeSet() and changing the outcome of compareTo() ...

Unrestricted change on a structured collection is not
a Good Thing. If you want mapcar, you know where to find it.

But what does a Lisp function have to do with Java?

Nothing. Read the above comment as: if you want Lisp functionality, use
Lisp.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFq2Fce+7xMGD3itQRAvibAJ0Ybl6rBHan7eAxcEM1P0i15Kh7lACdGaO/
w/GnXX5wjaeGPACVLazud1E=
=3Rdg
-----END PGP SIGNATURE-----
 
J

John Ersatznom

Hendrik said:
Nothing. Read the above comment as: if you want Lisp functionality, use
Lisp.

Lisp functionality? Lisp functionality is to tell you you have
unbalanced parentheses and leave you to find a needle in a haystack.
What he was looking for is myCollection collect: [:foo | foo + 1] which
is in a far nicer looking language. (But also a far slower one than
Java, unsuited for performing any serious work, last time I checked.)
 
E

Eric Sosman

John said:
Hendrik said:
Nothing. Read the above comment as: if you want Lisp functionality, use
Lisp.

Lisp functionality? Lisp functionality is to tell you you have
unbalanced parentheses and leave you to find a needle in a haystack.
What he was looking for is myCollection collect: [:foo | foo + 1] which
is in a far nicer looking language. (But also a far slower one than
Java, unsuited for performing any serious work, last time I checked.)

"The above comment" (snipped; it was "If you want mapcar, you
know where to find it") was about the nature of collections. Java
offers several kinds, some of which rely on characteristics of the
objects they contain: invariant hashCode(), consistent behavior of
compareTo() and so on. These structured collections cannot tolerate
arbitrary and uncontrolled changes to their contained objects (the
O.P. asked "what if you won't know what `change()' will be?").

Lisp's mapcar operator applies to a List, a kind of collection
whose structure does not rely on the nature of its contents but
only on their order of appearance. By bringing it up, I intended
two things: to appear clever (lost cause) by paraphrasing a well-
known flippancy, but also to provoke the O.P. to think about what
makes a Lisp List different from, say, a Java SortedSet. Java's
List is a reasonably close (albeit imperfect) analogue of Lisp's,
and a method that applies an arbitrary transformation to the contents
might make sense. (Note that Java does not prevent the programmer
from extending List's supplied implementations, nor from rolling his
own.) However, the O.P. asked for a much more general operation
that would apply to every kind of Collection -- and it was this goal
I wanted to call into question.
 
J

John Ersatznom

Eric said:
However, the O.P. asked for a much more general operation
that would apply to every kind of Collection -- and it was this goal
I wanted to call into question.

Yes. Even aCollection collect: aBlock creates a new one rather than
doing in-place modification. In Java you want to use ListIterator for
this as was already pointed out in greater detail elsewhere.

If you want an unordered collection you can either use a list (and not
care about the ordering) or use a map, iterate over the keys, and change
the values (or iterate over the entry objects and change the values).
Map doesn't care if values mutate or are replaced.
 
L

Lew

I'm sorry but all of you completely lost me. :(

Rather than bore you here, I will send you to Sun for boredom.

Er, wisdom.

<http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html>
<http://java.sun.com/j2se/1.5.0/docs/api/java/util/Iterator.html>
<http://java.sun.com/j2se/1.5.0/docs/api/java/util/ListIterator.html>
and related java.util interfaces and classes.

<http://java.sun.com/docs/books/tutorial/collections/index.html>
for the tutorial.

Our friends had wandered down a back alley of nuances that differentiate
collection types. In your case, they were saying you needed a ListIterator for
various reasons that will make more sense after you have read the above links
then re-read the posts in this thread.

- Lew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top