Re: learning clojure, converting a sequence with repetitions to a multi-map

2009-04-29 Thread Christophe Grand
IMHO, the slowdown comes from allocation: with (apply merge-with concat (map (fn [x] {(key-fn x) [x]}) s)) you build a map containing a vector, plus a seq (merge-with calls seq on each argument) for each item before performing a reduction which calls assoc and concat. In seq-to-multimap you o

Re: learning clojure, converting a sequence with repetitions to a multi-map

2009-04-29 Thread Boris Mizhen
Thanks Jason. merge-with seems to be made to support a function like this, I wonder where is the slowdown coming from? Is apply slow? I named your version seq-to-multimap2. The timing results are below: user> (def a (reverse (take 10 (iterate (fn [x] (rand-int 100)) 1 #'user

Re: learning clojure, converting a sequence with repetitions to a multi-map

2009-04-29 Thread Boris Mizhen
Thanks Christophe, > Using a default return value, you can rewrite the (if-let...) as (conj > (amap key ()) item). A good point, getting clojure and clojure :) (defn seq-to-multimap "takes a sequence s of possibly repeating elements and converts it to a map, where keys are obtained by app

Re: learning clojure, converting a sequence with repetitions to a multi-map

2009-04-28 Thread Jason Wolfe
(defn seq-to-multimap   "takes a sequence s of possibly repeating elements    and converts it to a map, where keys are obtained by applying key- fn    to elements of s and values are sequence of all elements of s with the particular key" [s key-fn] (apply merge-with concat (map (fn [x]

Re: learning clojure, converting a sequence with repetitions to a multi-map

2009-04-28 Thread Meikel Brandmeyer
Hi, Am 28.04.2009 um 23:28 schrieb Boris Mizhen: BTW, I hope I'm not abusing this mailing list by asking questions like this? This list and the #clojure channel are the right place to ask such questions. That said, there is the search facility of google groups and the log of the channel at ht

Re: learning clojure, converting a sequence with repetitions to a multi-map

2009-04-28 Thread Christophe Grand
Hi Boris, Boris Mizhen a écrit : > I am starting to learn clojure. I would appreciate comments on the > utility function below. > Coding style, idiomatic Clojure, comment style, efficiency, naming > conventions, indentations (used slime) ... anything I should > improve :) > > (defn seq-to-multima

Re: learning clojure, converting a sequence with repetitions to a multi-map

2009-04-28 Thread Boris Mizhen
Thanks Stuart, preserving order is a nice touch :) I also did not realize that conj preserves sequence type ... BTW, I hope I'm not abusing this mailing list by asking questions like this? Boris On Apr 28, 5:15 pm, Stuart Sierra wrote: > Hi Boris, welcome to Clojure! > > This function looks r

Re: learning clojure, converting a sequence with repetitions to a multi-map

2009-04-28 Thread Stuart Sierra
Hi Boris, welcome to Clojure! This function looks reasonable to me. In your example, you don't need to write "#(identity %)" -- just "identity" is enough. If you want to preserve the order of objects in the sequence, you can use a vector instead of a list. I would use "contains?" in the condit

learning clojure, converting a sequence with repetitions to a multi-map

2009-04-28 Thread Boris Mizhen
Hello all, I am starting to learn clojure. I would appreciate comments on the utility function below. Coding style, idiomatic Clojure, comment style, efficiency, naming conventions, indentations (used slime) ... anything I should improve :) (defn seq-to-multimap [s key-fn] "takes a sequence s