Yu Shen
Yu Shen

Reputation: 2910

Select element in collection satisfying max in Clojure

The following expression

(reduce (fn [[c x y] [s k d]] (if (< c s) [s k d] [c x y])) [0 0 0] colls)

Is for the element [e, x, y] in colls such that e is the maximum among all in the tuples in colls.

Is there already an idiomatic expression in Clojure for that? I guess that with macro, it should be possible to express it with brevity.

I feel that Python/numpy/panda has some good example.

Upvotes: 1

Views: 57

Answers (1)

Lee
Lee

Reputation: 144126

You can use max-key:

(apply max-key first colls)

Upvotes: 4

Related Questions