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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:28:33+00:00 2026-06-11T15:28:33+00:00

Trying to get a handle on pattern matching here– coming from a C++/Java background

  • 0

Trying to get a handle on pattern matching here– coming from a C++/Java background it’s very foreign to me.

The point of this branch is to check each member of a List d of tuples [format of (string,object). I want to define three cases.

1) If the counter in this function is larger than the size of the list (defined in another called acc), I want to return nothing (because there is no match)
2) If the key given in the input matches a tuple in the list, I want to return its value (or, whatever is stored in the tuple._2).
3) If there is no match, and there is still more list to iterate, increment and continue.

My code is below:

def get(key:String):Option[Any] = {

        var counter: Int = 0
        val flag: Boolean = false 

        x match {

                case (counter > acc) => None

                case ((d(counter)._1) == key) => d(counter)._2

                case _ =>   counter += 1
                }

My issue here is while the first case seems to compile correctly, the second throws an error:

:36: error: ')' expected but '.' found.
                case ((d(counter)._1) == key) => d(counter)._2

The third as well:

scala>         case _ =>  counter += 1
:1: error: illegal start of definition

But I assume it’s because the second isn’t correct. My first thought is that I’m not comparing tuples correctly, but I seem to be following the syntax for indexing into a tuple, so I’m stumped. Can anyone steer me in the right direction?

  • 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-11T15:28:34+00:00Added an answer on June 11, 2026 at 3:28 pm

    Hopefully a few things to clear up your confusion:

    Matching in scala follows this general template:

    x match {
      case SomethingThatXIs if(SomeCondition) => SomeExpression
      // rinse and repeat
      // note that `if(SomeCondition)` is optional
    }
    

    It looks like you may have attempted to use the match/case expression as more of an if/else if/else kind of block, and as far as I can tell, the x doesn’t really matter within said block. If that’s the case, you might be fine with something like

    case _ if (d(counter)._1 == key) => d(counter)._2
    

    BUT

    Some info on Lists in scala. You should always think of it like a LinkedList, where indexed lookup is an O(n) operation. Lists can be matched with a head :: tail format, and Nil is an empty list. For example:

    val myList = List(1,2,3,4)
    myList match {
      case first :: theRest => 
        // first is 1, theRest is List(2,3,4), which you can also express as
        // 2 :: 3 :: 4 :: Nil
      case Nil =>
        // an empty list case
    }
    

    It looks like you’re constructing a kind of ListMap, so I’ll write up a more “functional”/”recursive” way of implementing your get method.

    I’ll assume that d is the backing list, of type List[(String, Any)]

    def get(key: String): Option[Any] = {
      def recurse(key: String, list: List[(String, Any)]): Option[Any] = list match {
        case (k, value) :: _ if (key == k) => Some(value)
        case _ :: theRest => recurse(key, theRest)
        case Nil => None
      }
      recurse(key, d)
    }
    

    The three case statements can be explained as follows:

    1) The first element in list is a tuple of (k, value). The rest of the list is matched to the _ because we don’t care about it in this case. The condition asks if k is equal to the key we are looking for. In this case, we want to return the value from the tuple.

    2) Since the first element didn’t have the right key, we want to recurse. We don’t care about the first element, but we want the rest of the list so that we can recurse with it.

    3) case Nil means there’s nothing in the list, which should mark “failure” and the end of the recursion. In this case we return None. Consider this the same as your counter > acc condition from your question.

    Please don’t hesitate to ask for further explanation; and if I’ve accidentally made a mistake (won’t compile, etc), point it out and I will fix it.

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

Sidebar

Related Questions

I am trying to get the handle of a window from a process. Now,
I'm trying to get a better handle on using sockets asynchronously. According to this
I'm trying to get a handle on whether we have a problem in our
I am trying to get a handle on Solrnet and interacting an ASP.NET site
I'm trying to get a handle to the foreground window in C#/Net 2.0/WinForms by
I have been trying to get a handle on using MVC 4.0 with EF
I'm trying to get a barebone app engine app to handle incoming email. I've
I am trying to get a list of receivers that handle android.intent.action.BOOT_COMPLETED for other
I am trying to get the window handles to a Delphi application from an
I'm trying to get my head around observer pattern, and conceptually - I get

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.