Print result of a function

11,430

Solution 1

Try changing your code to something like this:

object poisson_sample_size extends App {
  val t1 = 0.0025
  val t2 = 0.0030

  val result = calculate(t1, t2)
  println(result)

  def calculate(theta1: Double, theta2: Double): Double = {         
    val num = 4
    val den = ((theta1 + theta2) / 2) - math.sqrt(theta1 * theta2)

    num / den       
  }
}

By making your object extend App, you get a main method for free. Then, using main is not a good idea for your custom calculation method as it creates confusion with a real main method, so I changed it to calculate. Then, you need to make sure your calculate method is being called, which is probably the real reason why you don't see anything getting printed in your current example.

Solution 2

Starting Scala 2.13, the chaining operation tap can be used as such:

import scala.util.chaining._

def f(): Int = calculate().tap(println)

to avoid such a construct:

def f(): Int = {
  val x: Int = calculate()
  println(x)
  x
}

The tap chaining operation applies a side effect (in this case println) on a value while returning the original value:

def tap[U](f: (A) => U): A

Solution 3

If I understand it correctly, to maintain the intended result for the function main you just need to insert x as the last statement in your function, which has an implicit return meaning in Scala.

object poisson_sample_size extends App {

  def main(theta1: Double, theta2: Double): Double = {
    val num = 4
    val den = ((theta1 + theta2) / 2) - math.sqrt(theta1 * theta2)

    val x = num / den
    println(x: Double)
    x // same as: return x
  }

  val theta1 = 0.0025
  val theta2 = 0.0030
  main(theta1, theta2)
}

In the way it is now, you should get a compile error message, as println has a Unit return type, but you declared main as having a Double return type.

Note that when you extend App you don't have to implement the main method. The actual body of the object is executed when the app is run. Try this modified version, please.

Share:
11,430

Related videos on Youtube

Gianluca
Author by

Gianluca

Sr Data Analyst in an ad tech firm. MSc in Economics and Finance. Passionate about Statistics and Machine Learning.

Updated on June 04, 2022

Comments

  • Gianluca
    Gianluca almost 2 years

    How can I change this function in Scala to be able, when running it on IntelliJ IDEA, to see the result?

    object poisson_sample_size extends App {
    
      def main(theta1: Double, theta2: Double): Double = {
    
        val theta1 = 0.0025
        val theta2 = 0.0030
    
        val num = 4
        val den = ((theta1 + theta2) / 2) - math.sqrt(theta1 * theta2)
    
        val x = num / den
        println(x: Double)
      }
    }
    

    I would like just to check the result is what I'm expecting it to be. I'm not sure it's error proof considering I just started learning Scala.

    I've trying to attribute the result of (num / den) to a variable and then print the variable itself but it doesn't do what I was expecting.

    • cmbaxter
      cmbaxter about 9 years
      Why don't you show the code where you are trying to print it
    • Gianluca
      Gianluca about 9 years
      @m-z: I would just println it, considering it's just a sort of debugging step I want to run to make sure the function returns the right results. @cmbaxter: I've edited the code to include the print statement.
  • Gianluca
    Gianluca about 9 years
    Thanks @Andre for your answer. I've changed main to return a Unit. Unfortunately this doesn't print on the console the value of x.
  • alcarv
    alcarv about 9 years
    @Gianluca now I see what you're trying to do. Please, consider this new version of the answer. You don't have to implement the main method when you extend App. I hope that helps.