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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:14:26+00:00 2026-05-31T05:14:26+00:00

I m trying to learn scala these days. I get confused with _ operator.

  • 0

I m trying to learn scala these days.

I get confused with _ operator.
How can I use it in the following program ?

Also how this program can be made more concise ?

I have learnt that scala promotes the use of val over var, in this case how can we use val for balance ?

private object Main {

  def main(args: Array[String]): Unit = {
    val acc1 = new PiggyBank(5)
    acc1.printBalance
    acc1 deposit 5
    acc1.printBalance
    acc1 withdraw 5
    acc1.printBalance
  }
}

private class PiggyBank(open_Bal: Int) {
  var balance = open_Bal
  def deposit(value: Int) = balance = balance + value
  def printBalance = println(balance)
  def iswithdrawable(value: Int) =  balance >= value
  def withdraw(value: Int) = {
    if (iswithdrawable(value)) {
      balance = balance - value
    }
  }
}

Thanks in Advance 🙂

  • 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-31T05:14:27+00:00Added an answer on May 31, 2026 at 5:14 am

    You’re probably going to get a million answers here 🙂 But, based on the content of your question, you need to read a book. I’d recommend Programming in Scala 2nd Edition. I’ve read it twice and it’s gotten dog-eared and coffee stained in the process.

    The reason I say this is because Scala presents you with a new paradigm, and you’re writing Java code in Scala. This is perfectly fine for starters but you’re not going to learn what you want to learn that way. The book is a great start that will give you the foundation to learn more.

    For example, here’s what I’d change in your code:

    case class PiggyBank(balance: Double) {
      def deposit(amount: Double) = PiggyBank(balance + amount)
      def withdraw(amount: Double): Option[PiggyBank] = {
        if (balance >= amount) Some(PiggyBank(balance - amount))
        else None
      }
      override def toString() = balance.toString
    }
    

    But “why” I would want to do it that way is the real question, and it’s that question that you really need to have answered, I argue. In a nutshell, it’s immutable and a bit more functional (although, this is a toy example and there’s tons of room for improvement here), but why is it and why do we care? Books answer this stuff.

    Given that, you can start to use the _ a bit if you want. For example:

    val result = PiggyBank(500) withdraw 200 flatMap { _.withdraw(200) } 
    println(result.getOrElse(0))
    

    But if you’re like most noobs (like me a long while ago), you’re going to ask “Why on earth is that better??”. That’s not an answer you’re going to find in a quick SO post. I could go on and on and on and on but the bottom line is that there are books out there that have already done that, and have done that better than I can.

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

Sidebar

Related Questions

Trying to implement, in Scala, the following Haskell function (from Learn You a Haskell...)
Info: I'm currently trying to learn template metaprogramming (by following this book ). One
Trying to learn a bit about PDO and is going through this tutorial .
I've been trying to learn about metaclasses in Python. I get the main idea,
I'm trying to learn about this feature of javascript I keep seeing in code,
I am trying to learn and use an SDK for a vendor's product. Unfortunately,
I'm trying to learn Scala and tried to write a sequence comprehension that extracts
I was trying learn lex and yacc using the oreilly book. I tried following
Trying to learn PL/SQL with multimedia data. Can anyone please point out that from
Trying to learn F# but got confused when trying to distinguish between fold and

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.