For Java.next [1,2] part 3, I am porting Daniel Spiewak's color
example [3] to Clojure. Here's the Clojure version:
(defstruct color :red :green :blue)
(defn red [v] (struct color v 0 0))
(defn green [v] (struct color 0 v 0))
(defn blue [v] (struct color 0 0 v))
(defn keys-with-value-matching [map test-fn]
(for [pair (map identity map) :when (test-fn (last pair))]
(first pair)))
(defn basic-colors-in [color]
(keys-with-value-matching color (comp not zero?)))
(defmulti color-string basic-colors-in)
(defmethod color-string [:red] [color] (str "Red: " (:red color)))
(defmethod color-string [:green] [color] (str "Green: " (:green color)))
(defmethod color-string [:blue] [color] (str "Blue: " (:blue color)))
(defmethod color-string :default [color]
(str "Red: " (:red color) ", Green: " (:green color) ", Blue:
" (:blue color)))
I am reasonably happy with the struct and the defmulti, but I am sure
there is a more elegant way to do basic-colors-in and especially keys-
with-value-matching. What is a more idiomatic approach?
Thanks,
Stuart
[1] http://blog.thinkrelevance.com/2008/8/4/java-next-common-ground
[2] http://blog.thinkrelevance.com/2008/8/12/java-next-2-java-interop
[3] http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-4
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---