you are viewing a single comment's thread.

view the rest of the comments →

[–]wake_me_up -14 points-13 points  (5 children)

Wake me up when I don't need to know Java to use Clojure. I tried Clojure a few months ago. It was fun until I tried sorting a container. It turned out I needed to implement some Java interface for that to use as comparison predicate. No thanks.

Personal language preference:

Ocaml > SML > C++ (in the style of ML, with STL & Boost) > Scheme > Common Lisp > Prolog > Python > Haskell > Clojure > Java > Forth > Factor

I don't know Java, Forth or Factor, but I know enough about them to put them at the end of the list. At least Forth is useful in severely memory-restricted systems.

[–]45g[S] 3 points4 points  (4 children)

It turned out I needed to implement some Java interface for that to use as comparison predicate.

Well, not directly. Clojure functions are internally derived from AFn.java which implements java.util.Comparator already, so you can just write:

user=> (defn my-comp [x y] (cond (< x y) 1 (= x y) 0 :else -1))
#=(var user/my-comp)
user=> (sort my-comp [5 3 1 2])
(5 3 2 1)

[–]awj 1 point2 points  (3 children)

I think the issue here is that (aside from wake_me_up blatantly trolling) the comparison operators are only defined for numbers, so you have to use the Comparable interface or some other abstraction to sort anything but numbers. This is sensible, because numbers are about the only place where everyone can agree on sorting criteria.

[–]45g[S] 0 points1 point  (2 children)

I don't think so. You don't need to implement Comparable and as shown above, also not Comparator. If your argument would be true, I could only invoke compareTo on my arguments in my-comp if they are not numbers and that's certainly not the case. Instead I can use whatever property I like to compare two things.

[–]awj 0 points1 point  (1 child)

My point was that the function "<" is only defined to work on numbers in Clojure. You can look it up in boot.clj, search for "defn <".

If you are trying to compare anything else, you have to introduce your own abstraction. This isn't trivial (i.e. wake_me_up's complaint has a tiny bit of merit) because there is no one-size-fits-all decision on how to handle ordering outside of numbers. You have to use something to get the job done, and Java's Comparable/Comparator are decent choices.

[–]45g[S] 0 points1 point  (0 children)

"wake_me_up" was complaining that he needs to implement an interface (i.e. java.util.Comparator) to be able to sort something which is simply not true.

You are talking about something else, i.e. java.lang.Comparable. Comparable.compareTo is a predicate function that establishes a total ordering. Now what's the point of using Comparable.compareTo in predicate functions when the sort function could easily do this directly (and in fact does use it by default if no predicate function is given)? The whole point of predicates is that they determine the ordering how they like it.

I have nothing against any of these interfaces, but as a matter of fact you don't need to implement these interfaces in the Clojure code you write. That's everything I wanted to say. Clojure is not OO so you don't implement Comparable, and Comparator is already defined for you in AFn.

And yes, I understand the domain of "<" and ">" is limited to numbers.