How powerful is Java?

Started by
47 comments, last by Alpha_ProgDes 11 years, 5 months ago
Any anything clojure does Groovy does better ;)....

http://groovy.codehaus.org/
Advertisement

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

http://groovy.codehaus.org/


And you have tried Clojure?
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):

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

I haven't used Groovy, but I can't imagine that it could be better than Clojure. tongue.png Anyway, I wouldn't inline Java in Clojure even if I could. Clojure's Java interop is very good. For example:
[source lang="java"]JButton okButton = new JButton("Ok");
okButton.setBounds(30, 35, 80, 25);
okButton.addActionListener(this);
panel.add(okButton);[/source]
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.

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):
[source](map #(cond (zero? (mod % 15)) "FizzBuzz" (zero? (mod % 3)) "Fizz" (zero? (mod % 5)) "Buzz" :else %) (range 1 101))[/source]
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.
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.

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. smile.png

- Jason Astle-Adams

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


[quote name='PurpleAmethyst' timestamp='1352397379' post='4998945']
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):
[source](map #(cond (zero? (mod % 15)) "FizzBuzz" (zero? (mod % 3)) "Fizz" (zero? (mod % 5)) "Buzz" :else %) (range 1 101))[/source]
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.
[/quote]
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.

Beginner in Game Development?  Read here. And read here.

 

This topic is closed to new replies.

Advertisement