Hi there,
I'm getting started with Clojure, and found myself really missing Scala-style pattern matching. Now I know about Matchure and core.match, but all I really needed was a cond using test predicates rather than boolean expressions, like this:
(condval value
foo-pred? (foo-result)
bar-pred? (bar-result)
else? (default-result))
(Where 'else? is a predicate that ignores the argument and returns true)
Lisp being Lisp, I rolled my own and it works fine.
Lisp being Lisp, I rolled my own and it works fine. But I was wondering if there was an idiomatic way to do this in the standard library, without the repetition that cond necessitates when you are testing against a single value.
How different was what you came up with from this?(def else? (constantly true))(defmacro condval [vname & body]`(cond
~@(interleave (map (fn [p] `(~p ~vname)) (take-nth 2 body))
(take-nth 2 (rest body)))))Just out of curiosity. :)
Hi there,
I'm getting started with Clojure, and found myself really missing Scala-style pattern matching. Now I know about Matchure and core.match, but all I really needed was a cond using test predicates rather than boolean expressions, like this:
(condval value
foo-pred? (foo-result)
bar-pred? (bar-result)
else? (default-result))
(Where 'else? is a predicate that ignores the argument and returns true)
Lisp being Lisp, I rolled my own and it works fine. But I was wondering if there was an idiomatic way to do this in the standard library, without the repetition that cond necessitates when you are testing against a single value.
Cheers,
Ken
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/ZLu3E-tr_Y4/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.