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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:17:13+00:00 2026-05-23T19:17:13+00:00

I am doing a pattern matching on a list. Is there anyway I can

  • 0

I am doing a pattern matching on a list. Is there anyway I can access the first and last element of the list to compare?

I want to do something like..

case List(x, _*, y) if(x == y) => true

or

case x :: _* :: y =>

or something similar…
where x and y are first and last elements of the list..

How can I do that.. any Ideas?

  • 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-23T19:17:14+00:00Added an answer on May 23, 2026 at 7:17 pm

    Use the standard :+ and +: extractors from the scala.collection package


    ORIGINAL ANSWER

    Define a custom extractor object.

    object :+ {
      def unapply[A](l: List[A]): Option[(List[A], A)] = {
        if(l.isEmpty)
          None
        else 
          Some(l.init, l.last)
      }
    }
    

    Can be used as:

    val first :: (l :+ last) = List(3, 89, 11, 29, 90)
    println(first + " " + l + " " + last) // prints 3 List(89, 11, 29) 90
    

    (For your case: case x :: (_ :+ y) if(x == y) => true)

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

Sidebar

Related Questions

What's the easiest way of doing simple pattern matching a la .something.com something.com/ something.com/somefolder/*.jpg
I'm doing a project which requires numerical pattern matching. For example i want to
I'm doing matching against some case classes and would like to handle two of
I'm at the moment doing some very basic pattern matching with quotations. My code:
Since many node.js scripts follow a pattern of doing something asynchronously (example below), how
I have no idea if there exists a pattern matching function for Common Lisp,
Doing my first SL4 MVVM RIA based application and i ran into the following
Doing my first tryouts with foreign keys in a mySQL database and are trying
I am doing some self learning about Patern Matching in Javascript. I got a
I'm pattern matching on the data constructor of a record, and I have the

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.