Several combinations of methods in a collection can be expressed more briefly in Scala. For example, xs.filter(f).headOptionyou can express how xs.find(f), but xs.map.filterusually it is better to express through xs.collect.
I find myself writing xs.sortWith(f).head, and it seems to me like something that can be expressed as a single method, "find me the smallest item in this collection, according to this sorting function."
However, I do not see obvious methods on Seqor TraversableLike. Is there one method that reflects my intention, or a .sort.headmore elegant way to find the "smallest" element?
source
share