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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:21:08+00:00 2026-05-30T19:21:08+00:00

How does one combine (in a nice way) two Scala match’es? First I have

  • 0

How does one combine (in a nice way) two Scala match’es?

First I have to test if an Option is a valid value:

myOption match {
  case Some(op) =>
    doSomethingWith(op)
  case None =>
    handleTheError()

Then if op was valid, I want to test for another pattern:

Path(request.path) match {
  case "work" => {
    println("--Let's work--")

  }
  case "holiday" => {
    println("--Let's relax--")
  }
  case _ => {
    println("--Let's drink--")
  }
}

I could combine them like this:

myOption match {
  case Some(op) =>
    doSomethingWith(op)
    Path(request.path) match {
      case "work" => {
        println("--Let's work--")          
      }
      case "holiday" => {
        println("--Let's relax--")
      }
      case _ => {
        println("--Let's drink--")
      }
    }
  case None =>
    handleTheError()

But, it feels sloppy. Is there a better way to combine them in some way or another.

Update

Apologies, I should have explained better. I’m actually trying to find out if there is a known pattern for simplifying (or factoring out) these control structures. For example (imagine this was true):

x match {
 case a => {
   y match {
    case c => {}
    case d => {}
   }
 }
 case b => {}
}

equals

x -> y match {
  case a -> c => {}
  case a -> d => {}
  case b => {}
}

I was just wandering if someone has already identified some refactoring patterns for control flow, much like algebra where 2(x + y) = 2x + 2y

  • 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-30T19:21:09+00:00Added an answer on May 30, 2026 at 7:21 pm

    You can do

    myOption map { success } getOrElse handleTheError
    

    or with scalaz,

    myOption.cata(success, handleTheError)
    

    where success is something like

    def success(op: Whatever) = {
      doSomethingWith(op)
      Path(request.path) match {
        case "work"    => println("--Let's work--")
        case "holiday" => println("--Let's relax--")
        case _         => println("--Let's drink--")      
      }
    }
    

    Update

    Your pseudocode

    x -> y match {
      case a -> c => {}
      case a -> d => {}
      case b => {}
    }
    

    can be literally translated to scala as

    (x, y) match {
      case (a, c) => {}
      case (a, d) => {}
      case (b, _) => {}
    }
    

    It looks nice (and that’s probably what you wanted) if inner matcher have only few options (c and d in this case), but it leads to code duplication (repeating of pattern a). So, in general I’d prefer map {} getOrElse {}, or separation of pattern-matchers on smaller functions. But I repeat, in your case it looks reasonable.

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

Sidebar

Related Questions

I have two .htaccess files that I need to combine into one. The first
How does one combine two GCC compiled .o object files into a third .o
I have a scenario where I combine two tables into one (using UNION) and
I have a data.table with two columns: one ID column and one value column.
I am currently trying to combine two collections into one for binding to a
How I can combine multiple form elements to one validator? I have address information
I have a class split across two files. One of these is generated, the
I am trying to combine three Byte values to one Long value, like System.Drawing.Color.ToArgb()
should we always combine one-to-one tables? I have a user id uid, a table
How does one combine using $ and point-free style? A clear example is 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.