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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:15:49+00:00 2026-06-07T13:15:49+00:00

I have several blocks of code that follow this pattern: // Dummy function defs.

  • 0

I have several blocks of code that follow this pattern:

// Dummy function defs.
def result(i : Int, d : Double, b : Boolean) = {
    if (b) d else i
}

def fA(s : String) = {7}
def fB(s : String, i : Int) = {1.0}
def fC(s : String, i : Int, d : Double) = {true}

// Actual code.
def test(s : String) : Double = {
    try {
        val a = fA(s) 
        try {
            val b = fB(s, a)
            try {
                val c = fC(s, a, b)
                result(a, b, c)
            } catch {
                case _ => result(a, b, false)
            }

        } catch {
            case _ => result(a, 0.0, false)
        }
    } catch {
        case _ => result(0, 0.0, false)
    }
}

Where a, b, & c are calculated in turn by the corresponding functions and then the values are passed to the result function. If at any stage an exception occurs then a default value is used in place of the remaining variables.

Is there a more idiomatic way to express this code. It reminds me of Monads in that it’s a series of chained computations which bail out immediately if any computation fails.

  • 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-07T13:15:53+00:00Added an answer on June 7, 2026 at 1:15 pm

    These types of problems are just what Try aims to solve a bit more monadically (than nested try/catch blocks).

    Try represents a computation that may either result in an exception, or return a successfully computed value. It has two subclasses for these– Success and Failure.

    Very funny that this question popped up when it did– a few days ago, I finished up some additions and refactoring to scala.util.Try, for the 2.10 release and this SO question helps to illustrate an important use-case for a combinator that we eventually decided to include; transform.

    (As of writing this, transform is currently in the nightly and will be in Scala from 2.10-M5 onward, due out today or tomorrow. More info about Try and usage examples can be found in the nightly docs)

    With transform (by nesting them), this can be implemented using Trys as follows:

    def test(s: String): Double = {
      Try(fA(s)).transform(
        ea => Success(result(0, 0.0, false)), a => Try(fB(s, a)).transform(
          eb => Success(result(a, 0.0, false)), b => Try(fC(s, a, b)).transform(
            ec => Success(result(a, b, false)), c => Try(result(a, b, c))
          )
        )
      ).get
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am maintaining several Perl scripts that all have similar code blocks for different
I have several blocks that look like this: <div class='templatechoicedesigncss'> <img src='/images/templatepics/random(100x140).png' /> <p>
I have several blocks of the following code that each use there own matrix.
We have a large C code base that has been developed over several decades.
I have a block of code that basically intializes several classes, but they are
I have a block of JSP code that needs to be used in several
I have several blocks of text that I need to be able to paste
Let's say I have some Jinja2 template with several blocks in it: {% block
I have several textboxes where users can enter information into them. This can include
I do have some strings that contains programming code, like XHTML (asp.net markup), C#,

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.