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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:12:44+00:00 2026-05-27T18:12:44+00:00

val total_breaks = //a random number total_breaks match { case i if(i < 0)

  • 0
val total_breaks = //a random number

total_breaks match {
  case i if(i < 0) => chartTemplate.setAttribute("totalBreaks", 0)
  case _ => chartTemplate.setAttribute("totalBreaks", total_breaks)
}

I was thinking there was a function in Scala that could shorten this. I thought min did this but I guess not. I can’t seem to find documentation on min, max, etc.

Something like total_breaks.min(0). Display 0 if under 0 if not display total_breaks.

Also is there a way do something like this

(4 + 5) match {
  case 0 => println("test")
  case _ => println(_) //i need to display the number passed into match?  Is this not  possible?
}

If I do case i => println(i) is that the same as case _ => ? Is that the fallback?

  • 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-27T18:12:44+00:00Added an answer on May 27, 2026 at 6:12 pm

    There are methods min and max defined in GenTraversableOnce, and thus available on sequences. You can use them as:

    scala> List(1, -4, 0).min
    resN: -4
    

    There is also min and max defined in RichInt, that work like operators on anything that can be converted to RichInt, typically your vanilla integers:

    scala> -4 min 0
    resN: -4
    

    So if you want something that returns your number, say x if x is greater than 0 and 0 otherwise, you can write:

    scala> x max 0
    

    That means you can rewrite your pattern-matching as:

    chartTemplate.setAttribute("totalBreaks", total_breaks max 0)
    

    For your second question, _ and i are both valid patterns that will match anything. The difference is that in the first case you do not bind what you have matched to a variable. Using println(_) is wrong, though; as such, it corresponds to an anonymous function that prints its first argument. So if you don’t want to repeat the expression (4 + 5), you should indeed write your pattern and code as:

    case i => println(i)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

implicit val odkaz = head; def vypis(implicit odkaz:Prvek):String = { odkaz match{ case null
this.String = { Get : function (val) { return function() { return val; }
I wonder if anyone could help me. I have a web page where there
I've written a jQuery function to check the total number of characters in a
var total = 0; $(.amount).each(function () { var value = $(this).val(); value = (value.length
I have code : function compute() { if ($('input[name=type]:checked').val() != undefined) { var a
Found this puzzle inside an image. According to my thinking the total number of
This my code: $('#paying_by').click(function() { if ($(this).val() == GBPTRF) { $('.currency').html('&#163 '); $('#c_card_rate').val(0.00); $('#c_card_amount').val(0.00);
I have a multidimentional array that is generated by: $('#order-submit').click(function(){ var orderResult = [];
I have the code below: $(input.isDebt).click(function() { var totalAmount_test = $(#CC_TotalAmount_form).val(); var total =

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.