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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:58:35+00:00 2026-06-14T07:58:35+00:00

While playing around in Scala, I came up against something that I think should

  • 0

While playing around in Scala, I came up against something that I think should be possible, but I don’t know how to do.

I’m returning a value that’s bounded by a given min/max. With an if-else statement, the function would look like this:

def set(n: Int, min: Int, max: Int): Int = 
{
  if (n < min) return min
  if (n > max) return max
  return n
}

I was wondering if it was possible to do this (elegantly) with pattern matching. I tried the following, but it was syntactically incorrect:

def set(n: Int, min: Int, max: Int): Int = n match 
{
  case (n < min) => min
  case (n > max) => max
  case _ => n
}

I think there’s a way to do it by mixing case and if statements, but by the time I’ve done that I might as well just be using a standard if/else chain. Is there a correct syntax to do what I’m attempting?

  • 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-14T07:58:36+00:00Added an answer on June 14, 2026 at 7:58 am

    Pattern matching works but is less elegant:

    def set(n: Int, min: Int, max: Int) = n match {
      case _ if n < min => min
      case _ if n > max => max
      case _ => n
    }
    

    because:

    def set(n: Int, min: Int, max: Int) = if (n < min) min else if (n > max) max else n
    

    (or if you like line breaks:

    def set(n: Int, min: Int, max: Int) = {
      if (n < min) min
      else if (n > max) max
      else n
    }
    

    )

    Return not needed.

    (Don’t forget math.min(max,math.max(min,n)), either.)

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

Sidebar

Related Questions

While playing around with regexps in Scala I wrote something like this: scala> val
just have a general question that came up while I was playing around with
While I was playing around with pdfbox 1.7.0 (apache) I came across a NullPointerException
While playing around with the emulator, I noticed that when trying to view a
Huh that's ironic ,while playing around today i wondered if a can Increase Int64.MaxValue
Possible Duplicate: stop php processing file Today while playing around with PHP a noticed
While playing around with some hooks I noticed that the output from the hooks
I've noticed, while playing around with it, that if you scroll up and down
I came across on a strange behavior of the following code, while playing around
While playing around with as' vector.sort() I found out that it behaves normally in

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.