How about defining seqable? in terms of whether seq works, using try catch?
Or better yet:
(defmacro if-seq
"If (seq obj) makes sense, evaluates expr1 with name bound to (seq obj);
otherwise evaluates expr2."
[[name obj] expr1 expr2]
`(let [[~name failed#] (try
[(seq ~obj) false]
(catch Throwable _# [~expr2 true]))]
(if failed# ~name ~expr1)))
user=> (if-seq [foo [1 2 3]] foo 0)
(1 2 3)
user=> (if-seq [foo (let [a (java.util.ArrayList.)] (.add a 17) a)] foo 0)
(17)
user=> (if-seq [foo "string"] foo 0)
(\s \t \r \i \n \g)
user=> (if-seq [foo 'bar] foo 0)
0
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---