This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-09-23
Channels
- # alda (1)
- # beginners (26)
- # boot (88)
- # carry (2)
- # cider (6)
- # clara (6)
- # cljs-dev (43)
- # cljsrn (14)
- # clojure (48)
- # clojure-belgium (2)
- # clojure-czech (4)
- # clojure-dev (1)
- # clojure-dusseldorf (7)
- # clojure-japan (1)
- # clojure-russia (55)
- # clojure-spec (65)
- # clojure-taiwan (1)
- # clojure-uk (28)
- # clojurescript (154)
- # cursive (5)
- # datomic (1)
- # editors (2)
- # emacs (29)
- # funcool (1)
- # jobs (3)
- # lambdaisland (5)
- # leiningen (1)
- # luminus (2)
- # new-channels (1)
- # off-topic (17)
- # om (18)
- # om-next (10)
- # onyx (24)
- # parinfer (14)
- # pedestal (4)
- # planck (3)
- # re-frame (69)
- # reactive (2)
- # reagent (3)
- # schema (2)
- # spacemacs (2)
- # sql (13)
- # vim (11)
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
(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
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
(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!
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]
(map
#(loop [cnt 0]
(when (< cnt num)
(println %)
(recur (inc cnt))))
lst))
3 [1 2 3 4])
which is kinda complicated in my opinion. Is there another shorter and more elegant way to solve this?@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!
Yeah, the important thing is to guess (as you did) that there must be a more elegant way 🙂
hello guys, is there any link to examples of clojure spec code?
I can see the vision but I still lack the experience