This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-10-29
Channels
- # bangalore-clj (2)
- # beginners (22)
- # boot (28)
- # cljs-dev (91)
- # cljsrn (4)
- # clojure (30)
- # clojure-austin (8)
- # clojure-conj (4)
- # clojure-japan (1)
- # clojure-russia (3)
- # clojure-spec (9)
- # clojure-uk (8)
- # clojurescript (42)
- # cursive (2)
- # editors (1)
- # hoplon (16)
- # klipse (91)
- # lein-figwheel (1)
- # luminus (9)
- # off-topic (4)
- # om (83)
- # onyx (29)
- # perun (6)
- # re-frame (17)
- # spacemacs (6)
- # untangled (1)
- # vim (5)
Guys, does this looks like an idiomatic Clojure? I’m mostly interested in keeping state in atoms
(defn threads->frames [coll]
(let [tmp (atom {:frames []})
index (atom 0)]
(reduce
(fn [acc item]
(if (contains? item :buttons)
(do
(swap! index inc)
(swap! tmp update-in [:frames] conj item)
(let [new-acc (conj acc @tmp)]
(reset! tmp {:frames []})
new-acc))
(do
(swap! index inc)
(swap! tmp update-in [:frames] conj item)
(if (= @index (count coll))
(conj acc @tmp)
acc))))
[]
coll)))
@olegakbarov: You could separate the updating of state, and the generation of the new version, I think it would make your code simpler. But I haven't fully understood your code yet. Doesn't feel very idiomatic to be messing with atoms repeatedly via a reduce though.
Yeah, same feelings
But i cant wrap my head around how to make this without this “statefulness"
(def input
[{:id 1 :buttons []}
{:id 2}
{:id 3 :buttons []}
{:id 4 :buttons []}
{:id 5}
{:id 6}])
=>
[[{:id 1 :buttons []}]
[{:id 2} {:id 3 :buttons []}]
[{:id 4 :buttons []}]
[{:id 5}{:id 6}]]
The idea here is to pack items with field :buttons
as last in batchwould be awesome to see!
@olegakbarov Sorry, meant you ping you: https://gist.github.com/rauhs/e16eceb726308443a98eb682f77d4af9
You'd then do:
(into []
(comp
(partition-after #(contains? % :buttons))
(map #(-> {:frames %})))
xx)
thanks! need some to for this to sink in
(defn new-threads->frames [coll]
(map
#(hash-map :frames %)
(reduce
(fn [acc item]
(let [h (butlast acc)
l (last acc)]
(if (contains? (last l) :buttons)
(concat acc [[item]])
(concat h [(concat l [item])]))))
[]
coll)))
Works for this specific case.Oops, @olegakbarov ^^
Oh, now i know about butlast
!
thanks