Fork me on GitHub

I’m trying to divide up a vector into a list of x vectors. I thought of using partition and partition-all but they all take the size I want each vector to be, but I’m trying to make x vectors, and have them be as close to the same size as possible. Basically if I have:

[1 2 3 4 5 6 7 8 9]  and want 5 groups I want them like ([1 2] [3 4] [5 6] [7 8] [9])
Is there a function similar to partition-all that would do this? I can’t use partition-all because my input list is of variable length so i don’t know how long I want each list to be, but I do know I want them to always be in x groups


(defn partition-x [x col]
  (partition-all (/ (count col) x) col))


I tried that, but since it comes out as a decimal and gives the wrong amount of groups sometimes. For example, with 15 items, 9 groups, that function gives me 8 groups


oh right, you need two groups of 1 at the end. that does seem a little tricky


yeah, it’s more like a "best-effort distribute coll amongst n lists"


(let [c1 (chan)
      c2 (chan)]
  (go (>! c1 :val1)
      (>! c2 :val2))
  [(<!! (go (alt! c1 ([v] [:result1 v]) 
                  c2 ([x y] [:result2 x y]))))
   (<!! (go (alt! c1 ([v] [:result1 v]) 
                  c2 ([x y] [:result2 x y]))))])


I had to try it out myself to figure out how it behaves, the docs aren't super clear. If the "operation" is of the form ([x] ,,,) then x will be bound to the value that came from the channel


if it's ([x y] ,,,) then x still receives the value, and y is bound to the channel


in this case in each branch we know exactly which channel was used, but you can also specifiy multiple channels in a vector, so then it will be useful to know which one was actually used


@dunn.mat Probably unidiomatic in some way since I’m a Clojure newbie, but:


(defn partition-x
  ([num-groups coll]
   (partition-x num-groups coll (count coll)))
  ([num-groups coll len]
   (if (= num-groups 1)
     (list coll)
     (let [group-size (quot (+ len num-groups -1)  num-groups)
           [first-group rest-coll] (split-at group-size coll)]
       (cons first-group
             (partition-x (dec num-groups) rest-coll (- len group-size)))))))


thank you @plexus!! All clear now 😉 So with your example, and the docs, I see that basically x ([v] v) gets you the value from the channel where x is just one single channel. I was confused, because [x y c] ([v] v) is also possible, for those cases in which you wouldn't care what channel was exactly the one you got the value from. I guess those cases exist, but the docs weren't really covering that case. Thanks again, it was very helpful!


my pleasure! come back if you have any more questions 🙂


Thanks 🙂 I will!


Hi everyone. I'm currently learning clojure and try some challenges on hackerrank. There was a challenge to print each element of an array n times. My solution looks like this

((fn [num lst]
     #(loop [cnt 0]
        (when (< cnt num)
          (println %)
          (recur (inc cnt))))
   3 [1 2 3 4])
which is kinda complicated in my opinion. Is there another shorter and more elegant way to solve this?


(defn repeat-print [num lst]
  (doseq [elt lst] (dotimes [n num] (println elt))))


Use doseq instead of map if you’re throwing away the result


And dotimes is the idiomatic way to perform that inner loop


@dfan this gives the right output, but I too wonder if it’s a little unidiomatic. I’ll play around at trying to simplify it but this is a good place to start. Thanks!


@dfan thank you, didn't know these two functions yet 🙂


Yeah, the important thing is to guess (as you did) that there must be a more elegant way 🙂


@snow a more "functional" solution


(run! println (mapcat (partial repeat 3) [1 2 3 4]))


hello guys, is there any link to examples of clojure spec code?


I can see the vision but I still lack the experience