Hi ka,

I do already use compare in my functions, but compare alone is costly
to performance and isn't as meaningful given it doesn't evaluate true/
false.
It's really just about convenience and code-readability while trying
not to sacrifice too much speed.

To give you an example here's my over-riding Clojure defaults in a
patch file:

(defn > [x y]
         (if (coll? y)
             (if (string? x)
                 (empty? (filter #(. clojure.lang.Numbers (isNeg (.compareTo
x %))) y))
                 (empty? (filter #(. clojure.lang.Numbers (gt % x)) y)))
       (if (string? x)
           (. clojure.lang.Numbers (isPos (.compareTo x y)))
           (. clojure.lang.Numbers (gt x y)))))

(defn < [x y]
         (if (coll? y)
             (if (string? x)
                 (empty? (filter #(. clojure.lang.Numbers (isPos (.compareTo
x %))) y))
                 (empty? (filter #(. clojure.lang.Numbers (lt % x)) y)))
       (if (string? x)
           (. clojure.lang.Numbers (isNeg (.compareTo x y)))
           (. clojure.lang.Numbers (lt x y)))))


Using these I can write shorter, more readable code. So let's compare
the two:

(if (> "2010-11-01"  ["2010-06-09" "2010-06-08" "2010-06-04"
"2010-06-10"])
    (run-my-function-to-upate-data))

vs.

(if (empty? (filter #(neg? (compare "2010-11-01" %))["2010-06-09"
"2010-06-08" "2010-06-04" "2010-06-10"]))
    (run-my-function-to-upate-data))

~ there's probably 10 different other ways to write the above, but
it's an example.

So while this may seem trivial to many, cumulatively I have quite a
few of these functions built that have helped me
not only cut my code size in half, but have made my code really easy
to read and maintain. Furthermore my rule is to
favour code readability and only optimize performance when
meaningfully required.

P.S.

It's also worth noting that while my version of > is uglier and longer
than say:

(defn > [x y]
         (if (coll? y)
             (empty? (filter #(. clojure.lang.Numbers (isNeg (.compareTo x
%))) y))
             (. clojure.lang.Numbers (isPos (.compareTo x y)))))

My version performs well. Using (compare n) on a number is much more
costly than doing a string check.
Since I use it as a library function I never have to see it's
ugliness.

:)

On Dec 3, 7:47 pm, ka <sancha...@gmail.com> wrote:
> Hi Tim,
>
> How about compare?
>
> user=> (compare "a" "b")
> -1
> user=> (compare "b" "b")
> 0
> user=> (compare "b" "a")
> 1
> user=> (compare 1 2)
> -1
> user=> (compare 2 2)
> 0
> user=> (compare 2 1)
> 1

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to