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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:45:29+00:00 2026-05-22T20:45:29+00:00

After wondering about certain bugs in my first Scala application, I discovered that my

  • 0

After wondering about certain bugs in my first Scala application, I discovered that my limit function was not quite working … at all!

So here was my first attempt:

  def limit(x : Double, min: Double, max : Double) = {
    if (x < min) min;
    if (x > max) max;
    x;
  }

It always returned x!

My second attempt looked like this:

  def limit(x : Double, min: Double, max : Double) : Double = {
    if (x < min) return min;
    if (x > max) return max;
    x;
  }

and it worked.

So my question: why are min; and max; from the first example basically no-ops, and x; is not?
And is my second attempt good Scala?

  • 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-22T20:45:29+00:00Added an answer on May 22, 2026 at 8:45 pm

    I’ve written a generic version of this (which I had called clamp), which looks like this:

    // NOTE: This will still do some boxing and unboxing because Ordering / Ordered is not @specialized.
    @inline def clamp[@specialized(Int, Double) T : Ordering](value: T, low: T, high: T): T = {
      import Ordered._
      if (value < low) low else if (value > high) high else value
    }
    

    In Scala, the result of an expression is the last value mentioned in that expression. The result of the if-expressions in your first attempt is thrown away, because each if-expression is followed by another expression. Your second attempt is ok but you could do it with if ... else like in my example. You could write yours like this:

    def limit(x: Double, min: Double, max: Double): Double =
      if (x < min) min else if (x > max) max else x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering how can I hide/remove a tag after a certain amount of
i was wondering how/where to write a callback function after an ajax request is
I'm just wondering how I could remove everything after a certain substring in PHP
I'm wondering about why I need to cut off the last 4 Characters, after
I've been wondering about this for a while after reading through an interesting article
I'm wondering about one thing : I want to ask the user while first
I'm wondering about the behind the scenes magic that's happening when you create a
I'm wondering about this: I have a simple facebook-connect app that will only show
I'm working on writing a web application using php & mysql, and was wondering
I'm starting my first Android app and after reading up I understand that 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.