The Artima Developer Community
Sponsored Link

Weblogs Forum
Will Closures Make Java Less Verbose?

70 replies on 5 pages. Most recent reply: Apr 10, 2008 9:09 AM by Mark Thornton

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 70 replies on 5 pages [ « | 1 2 3 4 5 | » ]
Joel Neely

Posts: 10
Nickname: joelneely
Registered: Mar, 2003

Re: Will Closures Make Java Less Verbose? Posted: Apr 1, 2008 8:11 PM
Reply to this message Reply
Advertisement
(I guess sorting employees is an idea whose time has come. ;-) I recently posted a small experiment (in Scala) based on that problem; it yielded a function that can be invoked as e.g.

sortBy ((e: Employee) => (e.first, e.id)) (employees)

to sort by first name and ID as a compound key. Using an anonymous function to compose a key tuple seems to provide the desired readability, but the implementation does the key extraction exactly once per sorted element.

http://joelneely.wordpress.com/2008/03/29/sorting-by-schwartzian-transform-in-scala/

Carson Gross

Posts: 153
Nickname: cgross
Registered: Oct, 2006

Re: Will Closures Make Java Less Verbose? Posted: Apr 1, 2008 8:35 PM
Reply to this message Reply
> (I guess sorting employees is an idea whose time has come.
> ;-) I recently posted a small experiment (in Scala) based
> on that problem; it yielded a function that can be invoked
> as e.g.
>
> sortBy ((e: Employee) => (e.first, e.id))
> (employees)

>
> to sort by first name and ID as a compound key. Using an
> anonymous function to compose a key tuple seems to provide
> the desired readability, but the implementation does the
> key extraction exactly once per sorted element.
>
> http://joelneely.wordpress.com/2008/03/29/sorting-by-schwar
> tzian-transform-in-scala/

Joel,

We were kicking this problem around today, trying to come up with something better than C# and thought of something similar: an overload of the sortBy() function that took a list of closures and sorted according to them. It would look something like this in GScript:

  var emps = getEmployees().sortBy( { \ e -> e.Name, \ e -> e.Salary }  )


That's a bit ugly, but it is pretty low rent. (In GScript, you can automatically create a new, strongly typed List or Array at a method parameter position by using {}'s, kind of like varargs)

If we added C# 3.0-like tuples to the language and made them Comparable if their constituent elements were Comparable, we could approach what you have above:

  var emps = getEmployees().sortBy( \ e ->  new {e.Name, e.Salary}  )


I still like the way Microsoft's solution reads the best though:

  var emps = getEmployees().sortBy( \ e -> e.Name).thenBy( \ e -> e.Salary} )


_shrug_

They all seem a lot better than what I have to do in Java today.

Cheers,
Carson

Jules Jacobs

Posts: 119
Nickname: jules2
Registered: Mar, 2006

Re: Will Closures Make Java Less Verbose? Posted: Apr 2, 2008 1:35 AM
Reply to this message Reply
Just make tuples or arrays comparable so you can do this:

xs.sortBy(\e -> [e.salary, e.name])

This works in Ruby:

xs.sort_by{|e| [e.salary, e.name]}

Like this:

def compare(xs,ys){
  for(i in 0..xs.length){
    if(xs[i] != ys[i]){
      return xs[i].compare(ys[i])
    }
  }
  return Equal
}

Rich Stone

Posts: 1
Nickname: regenerate
Registered: Apr, 2008

Re: Will Closures Make Java Less Verbose? Posted: Apr 3, 2008 10:55 AM
Reply to this message Reply
Why do we say that a language that has no verbs is verb-ose. Would it not be better to call it noun-ose - or at least wordy.

English is a language that has both parts of speech, and it is still mangled by most native speakers.

Thanks for the article, though. I agree that the java culture will probably overwhelm any new language features. While there are a few outspoken folks who see things in balance. We all know the maxim that:

"Culture eats strategy for lunch"

Eric Armstrong

Posts: 207
Nickname: cooltools
Registered: Apr, 2003

Re: Will Closures Make Java Less Verbose? Posted: Apr 3, 2008 12:11 PM
Reply to this message Reply
Thanks for pointing out Steve Yegge's post:
http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html

Parts of it made me fall down laughing...
The rest is just insightful.

The post also had an interesting pointer, as well:

* Nice: http://nice.sourceforge.net/language.html
A java-syntax, jvm-compatible language that makes
methods into first class citizens, which allows
for modules and closures, among other things.

That sure looks like Java3K, to me. (I've been saying we need such a thing for years. Once Java got to be a decade old, it seems to me that it was time.)

Many of the comments on that post are intriguing, too. Like this one:
"Java forces you to associate actions not with their
subjects, but most often with their objects"

That's a really interesting observation. A verb connects a
subject and an object. So do we have Steve.take_out(trash),
or Steve.trash.take_out()? Associating the method with the
object gives the latter. Associating it with the subject
gives the former.

But in a sense, take_out is really an /operator/ that joins
the two objects: take_out(Steve, trash)--only it reads better with infix dot-notation:

Steve.take_out.trash

Interestingly, the dot notation captures the idea that
sequence matters. So while plus(3,4) == plus(4,3),
infix space notation can be used: 3 + 4 == 4 + 3.

But take_out(trash, Steve) fails type checking (static or
dynamic), since the first argument must be an instance
of Person and the second must be an instance of Thing, so
dot-notation is better suited to expressing the concept.

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: Will Closures Make Java Less Verbose? Posted: Apr 3, 2008 12:59 PM
Reply to this message Reply
I thought the Yegge post was a good rant against bad Java code. And JavaBeans cause bad Java code. But it's possible to write bad code in any language. Most of the specific rants against Java were poor. e.g., as noted

>"Java forces you to associate actions not with their
>subjects, but most often with their objects"
...
>So do we have Steve.take_out(trash),
>or Steve.trash.take_out()?

How about trash.takeOut(Steve);

Usually there are at least two "reasonable" ways to do things as above - does Trash have takeOut or does Steve? And clearly Java doesn't *force* either of them, since I just wrote both versions.
In practice, I often write BOTH versions, and one just points to the other.

Patrick Viry

Posts: 1
Nickname: pviry
Registered: Apr, 2008

Re: Will Closures Make Java Less Verbose? Posted: Apr 3, 2008 3:46 PM
Reply to this message Reply
Carson wrote:

> No, closures will not make java less verbose, due to the
> following facts:

[...]

> * There is a suicidal commitment to non-local returns.

I have been thinking for some time about a proper term, "suicidal" is precisely what I was looking for, thank you for the vocabulary help. Adding a 'feature' that forever forbids code mobility just right when parallel/multicore programming is becoming unavoidable, well...

> * There is a comical commitment to "user defined control
> structures" which are perhaps the least useful, er, use of
> closures.

"comical" is well found either. For ages there has been only *one* use case (withLock) backing this idea.

> * There is a suicidal and comical commitment to allowing
> every possible variable capture semantic, with various and
> sundry notations to pick between them.

Indeed, the latest developments of the "consensus" are a creative combination of even more suicidal and even more comical. Precisely what is required to make the JavaOne 'rock stars' show a big success, which in turn is a good enough reason for not addressing these issues.

-Patrick

renghen renghen

Posts: 1
Nickname: renghen
Registered: Mar, 2008

Re: Will Closures Make Java Less Verbose? Posted: Apr 3, 2008 11:39 PM
Reply to this message Reply
improve the JVM in terms of performance. give programmers and admin a hand on the jvm on threading stack size, add end tail recursion in the jvm, provide runtime generics, etc...

from thereafter let language designer come up with language constructs. Thats my pick. Improve the jvm, let language designers come up with the syntax stuff.

as for me part, scala is going in the right direction and so F#(.net).

a language should be flexible, thus allow creation the easy creation of dsl.

Mayuresh Kathe

Posts: 2
Nickname: mayuresh
Registered: Sep, 2007

Re: Will Closures Make Java Less Verbose? Posted: Apr 4, 2008 2:05 AM
Reply to this message Reply
I think taking a break on backwards compatibility and delivering a fresh, new, well tuned Java 3 would be a great thing to happen to Java.

http://mayuresh.kathe.in/

Rafael Naufal

Posts: 3
Nickname: rnaufal
Registered: Jun, 2005

Re: Will Closures Make Java Less Verbose? Posted: Apr 4, 2008 9:52 AM
Reply to this message Reply
I've commented about Java backwards compatibilities and closures being implemented on the platform:

http://rnaufal.livejournal.com/17078.html
http://rnaufal.livejournal.com/12803.html

Eric Armstrong

Posts: 207
Nickname: cooltools
Registered: Apr, 2003

Re: Will Closures Make Java Less Verbose? Posted: Apr 4, 2008 10:12 AM
Reply to this message Reply
> ...
> >So do we have Steve.take_out(trash),
> >or Steve.trash.take_out()?
>
> How about trash.takeOut(Steve);
>
> Usually there are at least two "reasonable" ways to do
> things as above - does Trash have takeOut or does Steve?
> ...I often write BOTH versions, and one just points to the other.
>
I was thinking about it some more when I woke up this morning. It seems to me that there are actions an actor can do (subject), and actions that an object can have performed on it (object). So if take_out() is something I can do, then I can do that on any object that has that method. That's the essence of "duck typing", isn't it? So if trash() or pretty_girl have a take_out method, I can interface with them. (We're compatible). But since sumo_player doesn't have any take_out method that *I* can access, it just ain't going to happen.

I have no idea where that train of thought is headed, at them moment. (Probably off a cliff, at high speed.) But it did seem interesting...

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: Will Closures Make Java Less Verbose? Posted: Apr 4, 2008 11:50 AM
Reply to this message Reply
I haven't kept up with their latest developments, so I'm sure I'm mangling this, but the Naked Objects framework had a concept of defining a method that said (in GUI terms)

"you can drag an Object of type T onto me, and when you do so, I will call this method"

So, your Steve class would have two overloaded methods that I'll call accept

accept(Trash t)

accept(FashionModel fm)

Cameron Purdy

Posts: 186
Nickname: cpurdy
Registered: Dec, 2004

Re: Will Closures Make Java Less Verbose? Posted: Apr 6, 2008 8:55 PM
Reply to this message Reply
> To me, J3K would be:
> - drop primitives
> - make operators become methods
> - make classes become real objects (enabling
> meta-objects)
> - make methods become first-class members
> - shift control structures to API solutions
> - keep Java "look and feel"
> - keep interoperability with JVM-based languages

Along with "classes being real objects", any new JVM iteration should support:
- addition of "mix ins"
- no differentiation between classes, interfaces and mix-ins

The result would be a lot of the debate in the rest of this thread would be a simple mix-in to any collection, and voila, no debate ;-)

Peace,

Cameron Purdy | Oracle
http://www.oracle.com/technology/products/coherence/index.html

Cameron Purdy

Posts: 186
Nickname: cpurdy
Registered: Dec, 2004

Re: Will Closures Make Java Less Verbose? Posted: Apr 6, 2008 9:01 PM
Reply to this message Reply
> Abstracting operations over data structures is 95% of the
> benefit of closures for most of us coding dregs.

Yes, but for the DRY principle, you'd want both mix-ins and closures .. closures for one-offs and mix-ins for anything you need twice ;-)

Peace,

Cameron Purdy | Oracle
http://www.oracle.com/technology/products/coherence/index.html

Cameron Purdy

Posts: 186
Nickname: cpurdy
Registered: Dec, 2004

Re: Will Closures Make Java Less Verbose? Posted: Apr 6, 2008 9:05 PM
Reply to this message Reply
> Whut? Java provided a transition from C++ ? It provided a
> transition from Visual Basic or Delphi to Java.
>
> The only suitable word would be DOWNGRADE.

Such a strongly held opinion ..

Having used C++, Java and VB, I'd make one comment: Replacing an hour's GUI work in VB would take a day in Java and a week in C++, so I guess VB is far superior to both.

(Or perhaps the choice of language may depend on what the problem is that you're trying to solve ..)

Peace,

Cameron Purdy | Oracle
http://www.oracle.com/technology/products/coherence/index.html

Flat View: This topic has 70 replies on 5 pages [ « | 1  2  3  4  5 | » ]
Topic: Will Closures Make Java Less Verbose? Previous Topic   Next Topic Topic: Refactoring addicts and dynamic languages

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use