This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-08-23
Channels
- # babashka (104)
- # beginners (23)
- # calva (15)
- # cider (2)
- # clojure (29)
- # clojure-europe (14)
- # clojure-nl (2)
- # clojure-norway (3)
- # clojure-spec (4)
- # clojure-switzerland (1)
- # cursive (3)
- # datomic (6)
- # emacs (17)
- # etaoin (2)
- # expound (1)
- # fulcro (4)
- # graphql (4)
- # honeysql (7)
- # introduce-yourself (2)
- # jackdaw (5)
- # malli (3)
- # meander (19)
- # nbb (3)
- # off-topic (35)
- # pathom (6)
- # pedestal (4)
- # polylith (31)
- # rdf (11)
- # re-frame (8)
- # reitit (6)
- # shadow-cljs (8)
- # specter (4)
- # squint (15)
- # vim (6)
Hey clojurians, is there a way for group-by
to preserve ordering for larger collections?
Each group needs to be sorted. And I need the groups to be ordered by the first occurrence of the group-by key
Group by returns a map which is not ordered. There is a Java map that maintains this order. You could make your own group by which uses that collection
I ended up extracting the order from the collection first, and then sorting based off that. Something like this:
It’s not very efficient but the number of correspondents will usually be in the single digits
If it slows down in the future I’ll do as you suggested and implement group-by on top of array-map
(def d [{:version "160d388778a14d55b406689810e11f9f", :date "2022-08-15 17:07:56"}
{:version "160d388778a14d55b406689810e11f9f", :date "2022-08-15 17:07:56"}
{:version "160d388778a14d55b406689810e11f9f", :date "2022-08-16 18:00:00"} ;max
{:version "160d388778a14d55b406689810e11f9f", :date "2022-08-15 17:07:56"}
{:version "160d388778a14d55b406689810e11f9f", :date "2022-08-15 17:07:56"}
{:version "260d388778a14d55b406689810e11f9f", :date "2022-08-16 18:00:00"} ;max
{:version "260d388778a14d55b406689810e11f9f", :date "2022-08-15 17:07:56"}
{:version "260d388778a14d55b406689810e11f9f", :date "2022-08-15 17:07:56"}
{:version "360d388778a14d55b406689810e11f9f", :date "2022-08-15 17:07:56"}
{:version "360d388778a14d55b406689810e11f9f", :date "2022-08-19 17:07:56"} ;max
])
how can I get the max date with different version and identity data:
[{:version "160d388778a14d55b406689810e11f9f", :date "2022-08-16 18:00:00"}
{:version "260d388778a14d55b406689810e11f9f", :date "2022-08-16 18:00:00"}
{:version "360d388778a14d55b406689810e11f9f", :date "2022-08-19 17:07:56"}]
like datebase select version,date from d groupby version having max(date)
(->> (group-by :version d)
(map (fn [[_ rows]] (last (sort-by :date rows)))))
(->> (group-by :version d)
vals
(map #(last (sort-by :date %))))
does anyone knows a convenient equivalent to slurp in cljs? shadow-cljs's slurp is just readFileSync, I'm looking for something that can take urls as well, it's probably quite easy to write but first I want to make sure there isn't something already...
thanks, while reading I figured it will be hard to do since node allows for sync file read but not http requests so eith use the async file read and go all async or get a sync file read and async http request, either way I don't think I can mimic slurp api in cljs 😕
Isn't there a better way (more readable) to destructure this value of stack?
(let [stack (map second (reduce dwindle-1 () coll))]
(cond (empty? stack)
z
(empty? (rest stack))
(first stack)
:else
(tree-fold f z (reverse stack))))
I tried this but it doesn't work ( Can't have fixed arity function with more params than variadic function). I'm not sure it would even be better if it did work
(apply (fn
([] z)
([b] b)
([& stack] (tree-fold f z (reverse stack))))
(map second (reduce reduce-1 () coll)))
replace [& stack]
with [_ & stack]
and it will work. but that is not very effective and less readable solution than your cond
expression imho
the original code is a translation from pattern matching from another language.
is there any function in clojure which is very similar to javascript findIndex function ? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
something which find the index using the predicate ?
I am using (first (filter pred vector))
is there any better approach to do this ?