On Wed, Apr 14, 2010 at 9:33 AM, Jeff Rose <[email protected]> wrote:
> 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"})
>
for true switch statements (with constant values) there is case:
(map
(fn [[k v]]
(case k
:a (* 2 v)
:b (* 3 v)))
{:a 42 :b 17})
Form (doc case):
Unlike cond and condp, case does a constant-time dispatch, the
clauses are not considered sequentially. All manner of constant
expressions are acceptable in case, including numbers, strings,
symbols, keywords, and (Clojure) composites thereof. Note that since
lists are used to group multiple constants that map to the same
expression, a vector can be used to match a list if needed. The
test-constants need not be all of the same type.
hth,
Christophe
--
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.