Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7865651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:09:08+00:00 2026-06-03T00:09:08+00:00

Several combinations of methods on a collection can be expressed more succinctly in Scala.

  • 0

Several combinations of methods on a collection can be expressed more succinctly in Scala. For example, xs.filter(f).headOption can be expressed as xs.find(f), and xs.map.filter can usually be better expressed through xs.collect.

I find myself writing xs.sortWith(f).head, and this feels to me like the sort of thing that could be expressed as a single method, “find me the least element in this collection, according to this sorting function”.

However, I can’t see any obvious methods on Seq or TraversableLike. Is there a single method that captures my intent, or is .sort.head the more elegant way to find the “least” element?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:09:10+00:00Added an answer on June 3, 2026 at 12:09 am
    scala> val xs = List("hello", "bye", "hi")
    xs: List[java.lang.String] = List(hello, bye, hi)
    
    scala> xs.sortWith(_.length < _.length).head
    res10: java.lang.String = hi
    
    scala> xs.min(Ordering.fromLessThan[String](_ > _))
    res11: java.lang.String = hi
    
    scala> xs.min(Ordering.by((_: String).length))
    res12: java.lang.String = hi
    
    scala> xs.minBy(_.length)
    res13: java.lang.String = hi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several HTML code combinations like this: <h2></h2> <p></p> <iframe></iframe> <hr/> and from
Several of my overriden Equals methods have started throwing Invalid Cast Exceptions - Unable
I m giving a string that contains several different combination of data. For example
For several days now I've been messing around with Ajax calls using different combinations
I have some issues resolving an algorithm used to find all posible combinations taking
This is more a general purpose programming question than language specific. I've seen several
I have tried several combinations of ASCII, Latin1, Windows-1252, UTF-8 and Unicode to convert
I have several onmouseover events (combination of javascript functions and jquery accordions) but they
Several random customers get this exception every time I update my Android app. I've
Several months (maybe even a year or two) ago, I saw an asp .net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.