Fork me on GitHub
#beginners
<
2022-08-23
>
Zaymon01:08:20

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

hiredman01:08:25

Depends what you mean by ordering

hiredman01:08:49

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

👍 1
Zaymon01:08:12

Each group needs to be sorted. And I need the groups to be ordered by the first occurrence of the group-by key

dpsutton02:08:24

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

1
Zaymon02:08:54

I ended up extracting the order from the collection first, and then sorting based off that. Something like this:

Zaymon02:08:08

It’s not very efficient but the number of correspondents will usually be in the single digits

Zaymon02:08:19

If it slows down in the future I’ll do as you suggested and implement group-by on top of array-map

dpsutton02:08:14

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

1
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)
     vals
     (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)
            z

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

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

delaguardo12:08:13

replace [& stack] with [_ & stack] and it will work. but that is not very effective and less readable solution than your cond expression imho

1
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 ? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

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 ?