Hacker News new | ask | show | jobs
by mangler 5013 days ago
Clojure functions are Java classes. It's not much different from using a Java functional libs. Except that Java will probably be faster as Clojure still doesn't do primitives and stuff (not sure about that one, may be it already does)

You can do all the functional stuff in Java, Clojure is doing exactly that. It will be more verbose, but once you learn the ropes it just looks different, it is not different.

3 comments

here is some code i wrote last week: https://gist.github.com/3758511

it was a method i extracted from some old code somewhere else while i fixed a bug in it. the functional programmer in me sees opportunities to generalize (and prevent future bugs here), so i try to refactor it into a defaultdict and a higher order function, using guava. 20 minutes later I still don't have working code, i revert, curse the gods because this is a 30 second refactor even in python let alone clojure, and move on, i don't have time for this shit.

its not about it looking different. it doesn't matter that closures and objects are turing equivalent. its a syntax problem.writing functional java code has too much friction and is not worth it.

this is not even getting into how any hardcore functional code needs persistent data structures to be fast, which means a) you need to write your code against a 3rd party collections library instead of JDK provided implementations, and b) any interop with 3rd party code will now imply a conversion (copy) at the boundary.

I agree that Java can be awkward if you're trying to write using functional idioms.

Still, in case you were interested, I commented on your gist with a potential solution.

> It will be more verbose

Isn't it exactly why it is not good idea (or at least not convenient) to do so in Java, even if functional approach would be best suited for the task at hand?

Of course you can do everything in Java what you can do in Clojure. You can also manipulate strings and generate reports in an assembler, did you know this? It all boils down to how convenient it is.

Java was not designed for functional stuff, that's all. Saying that you can map a function over collection in Java isn't going to change this.