I just saw someone post a bunch of Clojure code to comp.lang.lisp one
function from which was:

(defn partition-with [pred coll]
  (loop [in coll curr [] out []]
    (if (empty? in)
      (conj out curr)
      (let [obj (first in)]
        (if (pred obj)
          (recur (rest in) [] (conj out curr))
          (recur (rest in) (conj curr obj) out))))))

This looks like it might be generally useful (and the CLL poster thought so
too); it splits a collection at items determined by the predicate, producing
a vector of vectors of items not matching the predicate:

user=> (partition-with odd? [3 4 8 7 9 2 4 6])
[[] [4 8] [] [2 4 6]]

(The empty collections are from before 3 and between 7 and 9, from the look
of it.

user=> (remove empty? (partition-with odd? [3 4 8 7 9 2 4 6]))
([4 8] [2 4 6])

gives what you might want instead, in some situations.)


The same CLL poster noticed that conj with two maps for arguments seems to
duplicate the functionality of merge. I've just tested this and it seems to
be true, even with key duplications:

user=> (merge {:key2 2 :key3 3} {:key1 1 :key4 4 :key3 7} )
{:key4 4, :key1 1, :key2 2, :key3 7}
user=> (conj {:key2 2 :key3 3} {:key1 1 :key4 4 :key3 7} )
{:key4 4, :key1 1, :key2 2, :key3 7}

This makes merge appear redundant.

Further experimentation shows that there isn't even a type-safety reason to
prefer merge. It doesn't throw on vectors. In fact:

user=> (merge [1 2 3] 4)
[1 2 3 4]
user=> (merge '(1 2 3) 4)
(4 1 2 3)
user=> (merge #{1 2 3} 8)
#{1 2 3 8}
user=> (merge #{1 2 3} 3)
#{1 2 3}
user=> (merge #{1 2 3} #{4 3})
#{1 2 3 #{3 4}}
user=> (conj #{1 2 3} #{4 3})
#{1 2 3 #{3 4}}

so merge appears to be strictly synonymous with conj. Curiouser and
curiouser...

--~--~---------~--~----~------------~-------~--~----~
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