• Create Account

## How powerful is Java?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

48 replies to this topic

### #41PurpleAmethyst  Members

Posted 08 November 2012 - 09:49 AM

Any anything clojure does Groovy does better ;)....

http://groovy.codehaus.org/

### #42tufflax  Members

Posted 08 November 2012 - 10:10 AM

Any anything clojure does Groovy does better ;)....

http://groovy.codehaus.org/

And you have tried Clojure?

### #43PurpleAmethyst  Members

Posted 08 November 2012 - 11:56 AM

I've never used Clojure on a commercial project. I have, however, used Groovy. I liked the fact I could inline JAVA and Groovy together. It helped get the job done very quickly.

Also, as you can see below, Groovy is less typing (Trivial example I know).

Groovy (70 Characters):

[source lang="groovy"]for (i in 1..100) { println "${i%3?'':'Fizz'}${i%5?'':'Buzz'}" ?: i }[/source]

Clojure (116 Characters):

(map #(cond (zero? (mod % 15)) "FizzBuzz" (zero? (mod % 3)) "Fizz" (zero? (mod % 5)) "Buzz" :else %) (range 1 101))

### #44tufflax  Members

Posted 08 November 2012 - 01:18 PM

[source lang="plain"](map #(condp (fn [d n] (= 0 (mod n d))) % 15 "FizzBuzz" 3 "Fizz" 5 "Buzz" %) (range 1 101))[/source]
91 characters.

I haven't used Groovy, but I can't imagine that it could be better than Clojure. Anyway, I wouldn't inline Java in Clojure even if I could. Clojure's Java interop is very good. For example:
becomes
[source lang="java"](.add panel (doto (JButton. "Ok") (.setBounds 30 35 80 25) (.addActionListener this)))[/source]

And even though Clojure is code not the shortest when it comes to small examples, the homoiconicity and simplicity of the syntax is very useful, and macros can get rid of a lot of code in longer programs.

Edited by tufflax, 08 November 2012 - 03:46 PM.

### #45Yrjö P.  Members

Posted 08 November 2012 - 01:46 PM

Also, as you can see below, Groovy is less typing (Trivial example I know).
Groovy (70 Characters):
[source lang="groovy"]for (i in 1..100) { println "${i%3?'':'Fizz'}${i%5?'':'Buzz'}" ?: i }[/source]
Clojure (116 Characters):

(map #(cond (zero? (mod % 15)) "FizzBuzz" (zero? (mod % 3)) "Fizz" (zero? (mod % 5)) "Buzz" :else %) (range 1 101))

Comparing apples to oranges. The Groovy one-liner is so clever it's no longer readable (nested ternary operators? please) and I'd complain about it in a code review, whereas both your and tufflax' Clojure implementations are clean code minus whitespace.

### #46PurpleAmethyst  Members

Posted 08 November 2012 - 05:52 PM

Yeah, I realize it was a bit trivial. Neither example is mine. I guess I'm not a big fan of LISP (yet) which doesn't help. I'm studying Haskell at the moment with some friends in the local hackerspace so my mind may change.

One thing I do like about Groovy is the ability to encode statements in the quotes using ${}. The in the above example isn't really a great demonstration of where it would be useful and I wouldn't be using nested ternary operators myself either, I'd complain about it too in a code review. ### #47jbadams Senior Staff Posted 08 November 2012 - 06:24 PM Debugging is atrocious and the tools for it are from the archaic at best and intentionally tortuous at worst. (compared to something like VS2010/2012). Try IntelliJ -- I haven't used it for quite some time, but last time I tried it was very good, and the other products from the same company are excellent. - Jason Astle-Adams ### #48Bacterius Members Posted 08 November 2012 - 06:40 PM I'm not sure why people are fighting over which language can produce the shortest one-liner to the fizzbuzz challenge, or any other problem for that matter. As far as I'm concerned it's the logic, concepts and train of thought behind the code that matter, rather than how many bytes I can encode my solution into. Sure, it's fun and all to compare, but unless said one-liners are readable, I don't want them in any code I look at (the Clojure code is quite nice, actually, but the Groovy one is at the edge). It's like that guy who implemented a basic ray tracer in 99 lines of ultra-compressed - and, quite frankly, unreadable without adding line breaks - C code. I just don't see the point. Every time I stumble across it, I die a little inside. I too can compress my code with 7z and end up with sub-1KB source code, big deal. Is it useful? No. Is it helpful to others? No. Does it warrant bragging rights? To people who want to understand the code, certainly not. Edited by Bacterius, 08 November 2012 - 06:40 PM. “If I understand the standard right it is legal and safe to do this but the resulting value could be anything.” ### #49Alpheus GDNet+ Posted 08 November 2012 - 06:42 PM Also, as you can see below, Groovy is less typing (Trivial example I know). Groovy (70 Characters): [source lang="groovy"]for (i in 1..100) { println "${i%3?'':'Fizz'}\${i%5?'':'Buzz'}" ?: i }[/source]
Clojure (116 Characters):

(map #(cond (zero? (mod % 15)) "FizzBuzz" (zero? (mod % 3)) "Fizz" (zero? (mod % 5)) "Buzz" :else %) (range 1 101))

Comparing apples to oranges. The Groovy one-liner is so clever it's no longer readable (nested ternary operators? please) and I'd complain about it in a code review, whereas both your and tufflax' Clojure implementations are clean code minus whitespace.

I'm sorry but that Groovy isn't that clever (ie. terse) and it's very readable. And actually it's easier to read the Groovy than the Clojure. As far as Scheme-like languages go, I've used Racket, as a learning tool. And I think it's great.

But to get back on topic, Java as a language or a platform can address all the needs a developer or business may have.
External Articulation of Concepts Materializes Innate Knowledge of One's Craft and Science