Fork me on GitHub

Hey clojurians, is there a way for group-by to preserve ordering for larger collections?


Depends what you mean by ordering


Group-by does preserve the order of elements in each group

👍 1

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


Turn ordering into a map from id to index. Don't call index of n times

quan xing06:08:02

(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)

Martin Půda07:08:26

(->> (group-by :version d)
     (map (fn [[_ rows]] (last (sort-by :date rows)))))

(->> (group-by :version d)
     (map #(last (sort-by :date %))))

quan xing07:08:58

Wow! It's very simple. I try to use reduce , but I failed. Thank you!

Lidor Cohen07:08:54

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...

Lidor Cohen09:08:40

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 😕

Jim Newton12:08:54

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)

            (empty? (rest stack))
            (first stack)

            (tree-fold f z (reverse stack))))

Jim Newton12:08:16

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

Jim Newton13:08:29

the original code is a translation from pattern matching from another language.

Muhammad Hamza Chippa14:08:28

is there any function in clojure which is very similar to javascript findIndex function ?

Muhammad Hamza Chippa15:08:05

something which find the index using the predicate ?

Muhammad Hamza Chippa15:08:03

I am using (first (filter pred vector)) is there any better approach to do this ?