I've found two convenience methods to be of use to me in my project,
and I'm not certain where I ought to share them. So, I thought I'd
share them here, for your consideration. Sorry, I'm a bit of a n00b to
Clojure. :-)

The first I would suggest for inclusion in core.clj; it is very
similar in behavior to "some", but returns a boolean like "every?".

(defn any?
  "Returns true if (pred x) is logically true for one x in coll, else
false."
  {:added "1.3" :tag Boolean}
  [pred coll]
  (when (seq coll)
    (or (pred (first coll)) (recur pred (next coll)))))

The second function is suggested as an addition to clojure.set. The
"disjoint?" function decides if two sets have no elements in common.
This can easily be done using:

  (not (nil? (intersection s1 s2)))

but this implementation should be more efficient (I think) and is more
readable, imho:

(defn disjoint?
  "Is set1 disjoint from set2?"
  {:added "1.3" :tag Boolean}
  [set1 set2]
  (if (<= (count set1) (count set2))
    (recur set2 set1)
    (not-any? (fn [item] (contains? item set1)) set2)))

Feel free to use these, or, how best should I submit these?

Cheers,
Travis

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to