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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:25:09+00:00 2026-05-24T00:25:09+00:00

What is the best Scala idiom for matching a value to the n-th element

  • 0

What is the best Scala idiom for matching a value to the n-th element of a list?

The naive approach obviously doesn’t work:

scala> val list = List(5,6,7)
list: List[Int] = List(5, 6, 7)

scala> val x = 7
x: Int = 7

scala> x match { case list(2) => true; case _ => false }
<console>:10: error: value list is not a case class constructor, nor does it have an       unapply/unapplySeq method
   x match { case list(2) => true; case _ => false }

To clarify- this question is not about how to compare a value to the n-th element of a list – it is specifically about whether it can be done using matching.

  • 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-24T00:25:09+00:00Added an answer on May 24, 2026 at 12:25 am

    Behold, the power of instance extractors! (the Regex class in the stdlib works similarly)

    case class Nth[A](which: Int) {
      def unapply(in: List[A]): Option[A] = if (in.size >= which+1) Some(in(which)) else None
    }
    
    val second = Nth[Int](1)
    
    List(2,4,6) match {
      case second(4) => println("yep!")
      case x => println("nope!")
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way in Scala to convert: Option[Int] to: Option[Long]
What's the best way to recursively search for an element in scala 2.10 ASTs?
What's the best way to convert a List of Lists in scala (2.9)? I
What is the best non-ORM database to work with Scala? I find this link
Which Scala ORMs fit and work best with the following requirements? Sufficiently typesafe. Does
Using Scala, what is the best approach for converting <myapp> <username>bill</username> <password>secret123</password> <background>#FFFFFF</background> </myapp>
Which build tool is the best for Scala? What are the pros and cons
Best way to match 2 pages for Google analytics. Pages to Query: 1.) this_google.html
I am new to Scala. Which database connectivity is best supported in Scala? I
Using Scala 2.8 RC1 or newer, what is the best (easiest and/or most direct)

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.