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 6852547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:23:03+00:00 2026-05-27T01:23:03+00:00

So I accidentally wrote a Haskell answer to a Scala question recently. Being rather

  • 0

So I accidentally wrote a Haskell answer to a Scala question recently. Being rather familiar with Haskell, the solution came quite easily to me:

myMaxBy :: (a -> a -> Ordering) -> [a] -> [a]
myMaxBy _ [] = undefined
myMaxBy f (x:xs) = foldr step [x] xs
  where step y acc@(z:_) = case f y z of
          GT -> [y]
          EQ -> y:acc
          LT -> acc

Then someone reminded me that it was a Scala question. I set out to transate my code into Scala, and after much pain I settled for:

(List(xs.head) /: xs.tail) { (acc, y) =>
  y compare acc.head match {
    1  => List(y)
    0  => y :: acc
    -1 => acc
  }
}

But I could not for the life of me get the Scala type system to bend to my will and generalize this into a function where xs and compare are inputs (ideally, curried inputs with the comparator first). Though this is surely due to my general unfamiliarity with Scala, I also slightly blame Scala’s complex (though very powerful) type system. Can you do some hand-holding and walk me through how I could turn this into a generalized function, with a type signature similar to the Haskell equivalent? (Read: as general as.) Please also demonstrate usage, if it is more complicated than myMaxBy(myCompare)(someList).

  • 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-05-27T01:23:04+00:00Added an answer on May 27, 2026 at 1:23 am

    You missed out the case keywords in your pattern-match, which are quite important!

    All you need is for the collection parameter type to be able to use a compare method. There are two systems for comparing things in Scala: extending Ordered, or using an Ordering. There are implicit conversions between the two so it doesn’t matter too much which you choose; the first is perhaps easier to understand.

    First off, using Ordered:

      def myMaxBy[A <% Ordered[A]](xs: List[A]) = {
        (List(xs.head) /: xs.tail) { (acc, y) =>
          y compare acc.head match {
            case 1  => List(y)
            case 0  => y :: acc
            case -1 => acc
          }
        }  
      }
    

    Here we give our generic type A a view bound using <%, which means “can be seen as”. This is more general than using an upper bound <:, and useful for classes that are not themselves Ordered, but have implicit conversions to Ordered classes, e.g. Int to RichInt.

    Alternatively, if you want flexibility to be able to change the ordering criteria, you can write it like this:

      def myMaxBy[A](xs: List[A])(implicit ord: Ordering[A]) = {
        (List(xs.head) /: xs.tail) { (acc, y) =>
          ord.compare(y, acc.head) match {
            case 1  => List(y)
            case 0  => y :: acc
            case -1 => acc
          }
        }
      }
    

    When invoking, if there’s an implicit Ordering[A] in scope, you can leave the second argument out. This second way also has the advantage that you can define an Ordering on arbitrary classes, whether or not they already support it.

    You can invoke both using, for example, myMaxBy(List(1,2,3,4,3,4)). In the second, if you wanted, say, reverse ordering: myMaxBy(List(1,2,3,4,3,4))(Ordering.Int.reverse).

    Another thing you might see in this context are context bounds. E.g. [A: Ordering]. This means the same as [A](implicit ord: Ordering[A]), which is more concise, except that you don’t get a handle on the Ordering so have to summon it using implicitly. So here it’s probably best to state it explicity as above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I accidentally wrote this: do { } while (true); ...and then ran
accidentally at work I wrote the following line of code: string x = (object)
I recently wrote a post: Weird Error in C++ Program: Removing Printout Breaks Program
I wrote a python script but accidentally put an infinite while loop in my
I accidentally wrote a java statement with two semicolons at the end. The java
When trying to do this: setTimeout(function(){alert(Boo);}, 500); I accidentally wrote this: setTimeout(new function(){alert(Boo);}, 500);
I recently wrote a quick-and-dirty proof-of-concept proxy server in C# as part of an
Well I'm a bit of an idiot, I wrote a view then accidently closed
Accidentally I may forget to describe some parameters or exception throwing (or something else)
Accidentally I mixed up between 32Bit and 64Bit library files on my x86 embedded

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.