On Apr 13, 11:34 pm, strattonbrazil <[email protected]> wrote:
> I want to map a dictionary and do different things depending on the
> key. I was planning on using an if-clause, but I'm not sure how to
> compare symbols to strings.
>
> Something like
>
> (map (fn [k v] (if (== k "hello") ... ...) {:hello 1 :goodbye 2})
>
> How would I normally compare symbols and strings?
I recently discovered condp, which is good for doing the equivalent of
a switch statement and more:
(map
(fn [[k v]]
(condp = k
:a (* 2 v)
:b (* 3 v)))
{:a "foo" :b "bar"})
Notice also that you need to put the [k v] inside an additional pair
of brackets in order to destructure the key/value pair. Otherwise the
anonymous function would just expect two arguments, rather than
destructuring a single argument.
(keyword "foo") => :foo
(name :foo) => "foo"
-Jeff
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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
To unsubscribe, reply using "remove me" as the subject.