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

  • Home
  • SEARCH
  • 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 8350275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:15:58+00:00 2026-06-09T08:15:58+00:00

I asked an earlier question to which I received a great answer. In the

  • 0

I asked an earlier question to which I received a great answer. In the comments, Travis mentioned that comparing two HandValues wouldn’t work directly, but pattern matching could be used to ensure comparing the same class.

  sealed abstract class HandValue[T <: HandValue[T]](val rank: Int) extends Ordered[T]
  case class HighCard(high: Int) extends HandValue[HighCard](0){
    def compare(that: HighCard) = this.high - that.high
  }

  case class TwoPair(high: Int, big: Int, sm: Int) extends HandValue[TwoPair](2) {
    def compare (that: TwoPair) = { ... }
  }

In the attempted pattern matching below, I have a compile-time error that I suspect has to do with using HandValue[_]. val h1: HandValue[T <: HandValue[T]], similar to how the type was declared, isn’t valid. Is there a way to resolve these?

  val ans = sessions count {
    hands: (Hand, Hand) => {
      val h1: HandValue[_] = handValue(hands._1)
      val h2: HandValue[_] = handValue(hands._2)
      (h1, h2) match { // <-- Line as source of error
        case _ if h1.rank > h2.rank => true
        case (a: HighCard, b: HighCard) => a > b
        case (a: TwoPair, b: TwoPair) => a > b
        // etc..
      }
    }
  }

Edit: The compile-time error is:

error: type arguments [_$3] do not conform to class HandValue's type parameter bounds [T <: euler.solutions.p54.HandValue[T]]
(h1, h2) match {

Edit 2: As mentioned in this question, using Type[_] won’t work.

  • 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-09T08:15:59+00:00Added an answer on June 9, 2026 at 8:15 am

    Can’t help with the error, but you could remove a lot of this complexity by allowing any HandValue to be compared to any other HandValue; then you don’t have to have that horrendous parameterization and repeated compare methods, followed by the repeated comparison logic in ans.

    One way would be to have each one define a strength, which is a Seq[Int] consisting of the hand rank followed by the ranks of the cards within the hand that define its strength. Then you just compare these Seqs by finding which has a larger number coming first, i.e.

    sealed abstract class HandValue(val strength: Seq[Int]) extends Ordered[HandValue] {
      import math.Ordering.Implicits.seqDerivedOrdering
      def compare(that: HandValue) = 
        Ordering[Seq[Int]].compare(this.strength, that.strength)
    }
    
    case class HighCard(high1: Int, high2: Int, high3: Int, high4: Int, high5: Int ) 
      extends HandValue(Seq(0, high1, high2, high3, high4, high5))
    
    case class TwoPair(high: Int, big: Int, sm: Int) 
      extends HandValue(Seq(2, big, sm, high))
    
    val ans = sessions count { hands => 
      handValue(hands._1) > handValue(hands._2)
    }
    

    Note, you need to take all the cards into account when calculating a high card hand strength. Also look out for the Ace-to-5 straight!

    You could also just calculate the strength as an Int using a hashing function (as I did when I did this problem: https://gist.github.com/3270831).

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

Sidebar

Related Questions

This question relates to this question which I asked earlier this week. The answer
I asked a question earlier which elicited some great responses. Here's the earlier question
Disregarding my question I asked earlier (and deleted) which has been solved, I want
Related to a question I asked earlier here , I've found a problem which
Earlier I asked this question , which basically asked how to list 10 winners
I asked this question earlier: How to get delta between two text items But
I asked an earlier question which was definitely helpful and let me know about
This is a follow-up to a question I'd asked earlier which phrased this as
Earlier I asked this question which was on how to make my own set
I asked a question earlier about which method for saving would be better, I

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.