This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-08-02
Channels
- # announcements (11)
- # aws (3)
- # babashka (34)
- # beginners (20)
- # biff (2)
- # calva (3)
- # cherry (29)
- # cider (6)
- # cljs-dev (9)
- # clojure (124)
- # clojure-europe (12)
- # clojure-norway (5)
- # clojure-uk (2)
- # clojurescript (32)
- # conjure (11)
- # datalevin (1)
- # datomic (16)
- # deps-new (1)
- # etaoin (6)
- # holy-lambda (10)
- # honeysql (28)
- # hyperfiddle (21)
- # jackdaw (2)
- # jobs (2)
- # leiningen (15)
- # missionary (12)
- # off-topic (132)
- # other-languages (1)
- # pathom (13)
- # rdf (10)
- # re-frame (8)
- # reagent (5)
- # releases (1)
- # remote-jobs (4)
- # shadow-cljs (32)
- # tools-deps (6)
- # vim (15)
- # xtdb (24)
i have a collection and i want to partition it to two colls based on if the index is divisible by 2. What is the correct/easiest way to do this?
Well, you could for instance use map-indexed
to explicitly get a sequence of [idx val]
pairs, then it should be quite easy to get to the desired output.
(let [xs [0 1 2 3 4 5]
with-index (map-indexed vector xs)]
(-> (group-by (fn [[idx _]] (rem idx 2)) with-index)
(update-vals #(map second %))))
(let [xs [0 1 2 3 4 5]]
[(take-nth 2 xs)
(take-nth 2 (rest xs))])
=> [(0 2 4) (1 3 5)]
Thanks!
The solution with take-nth
is really interesting - nice reminder there's such a function 🙂
But I prefer group-by
- I think it's closer to the problem description:
(let [xs [0 1 2 3 4 5]]
(vals (group-by even? xs)))
;; => ([0 2 4] [1 3 5])
Partitioning by the index of the element being even/odd
If a function has :arglists
metadata, should that be considered the "main" arglist definition over the actual arglist?
I don't think this is a beginners question (more #clojure or even #clojure-dev) but, yes
I have two vec of vecs, how do I extend the subvecs into the first coll. ie.
(conj [[1 2] [3 4] [5 6]] [[2 2] [4 4]])
googled with "extend" and eyeballed the cheatsheet and didnt notice that. Thanks alot!
I'm not sure I understand what you want. Do you mean
(apply conj [[1 2] [3 4] [5 6]] [[2 2] [4 4]])
?concat was the function I was looking for
(reduce conj [[1 2] [3 4] [5 6]] [[2 2] [4 4]])
this didnt seem so natural