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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:37:45+00:00 2026-05-19T15:37:45+00:00

Often I face following situation: suppose I have these three functions def firstFn: Int

  • 0

Often I face following situation: suppose I have these three functions

def firstFn: Int = ...
def secondFn(b: Int): Long = ...
def thirdFn(x: Int, y: Long, z: Long): Long = ...

and I also have calculate function. My first approach can look like this:

def calculate(a: Long) = thirdFn(firstFn, secondFn(firstFn), secondFn(firstFn) + a)

It looks beautiful and without any curly brackets – just one expression. But it’s not optimal, so I end up with this code:

def calculate(a: Long) = {
  val first = firstFn
  val second = secondFn(first)
  
  thirdFn(first, second, second + a)
}

Now it’s several expressions surrounded with curly brackets. At such moments I envy Clojure a little bit. With let function I can define this function in one expression.

So my goal here is to define calculate function with one expression. I come up with 2 solutions.

1 – With scalaz I can define it like this (are there better ways to do this with scalaz?):

  def calculate(a: Long) = 
    firstFn |> {first => secondFn(first) |> {second => thirdFn(first, second, second + a)}}

What I don’t like about this solution is that it’s nested. The more vals I have the deeper this nesting is.

2 – With for comprehension I can achieve something similar:

  def calculate(a: Long) = 
    for (first <- Option(firstFn); second <- Option(secondFn(first))) yield thirdFn(first, second, second + a)

From one hand this solution has flat structure, just like let in Clojure, but from the other hand I need to wrap functions’ results in Option and receive Option as result from calculate (it’s good it I’m dealing with nulls, but I don’t… and don’t want to).

Are there better ways to achieve my goal? What is the idiomatic way for dealing with such situations (may be I should stay with vals… but let way of doing it looks so elegant)?

From other hand it’s connected to Referential transparency. All three functions are referentially transparent (in my example firstFn calculates some constant like Pi), so theoretically they can be replaced with calculation results. I know this, but compiler does not, so it can’t optimize my first attempt. And here is my second question:

Can I somehow (may be with annotation) give hint to compiler, that my function is referentially transparent, so that it can optimize this function for me (put some kind of caching there, for example)?

Edit

Thanks everybody for the great answers! It’s just impossible to select one best answer (may be because they all so good) so I will accept answer with the most up-votes, I think it’s fair enough.

  • 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-19T15:37:46+00:00Added an answer on May 19, 2026 at 3:37 pm

    in the non-recursive case, let is a restructuring of lambda.

    def firstFn : Int = 42
    def secondFn(b : Int) : Long = 42
    def thirdFn(x : Int, y : Long, z : Long) : Long = x + y + z
    
    def let[A, B](x : A)(f : A => B) : B = f(x)
    
    def calculate(a: Long) = let(firstFn){first => let(secondFn(first)){second => thirdFn(first, second, second + a)}}
    

    Of course, that’s still nested. Can’t avoid that. But you said you like the monadic form. So here’s the identity monad

    case class Identity[A](x : A) {
       def map[B](f : A => B) = Identity(f(x))
       def flatMap[B](f : A => Identity[B]) = f(x)
    }
    

    And here’s your monadic calculate. Unwrap the result by calling .x

    def calculateMonad(a : Long) = for {
       first <- Identity(firstFn)
       second <- Identity(secondFn(first))
    } yield thirdFn(first, second, second + a)
    

    But at this point it sure looks like the original val version.

    The Identity monad exists in Scalaz with more sophistication

    http://scalaz.googlecode.com/svn/continuous/latest/browse.sxr/scalaz/Identity.scala.html

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

Sidebar

Related Questions

I face a situation where we have many very long methods, 1000 lines or
I face this often. Suppose I have killed a buffer. And then I go
Often I find myself in a situation where I have to deal with catching
I often run in the following problem, I have a web page with the
I often face the problem of wanting to add additional methods to classes I
This is non-language-specific, but I'll use examples in C# . Often I face the
Often I have a function of such pattern: f :: a -> b f
Often I face the problem of mapping the parameter space of one API onto
Often you have to implement a collection because it is not present among those
I often face this problem with git: I clone a git repo of some

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.