I'm working on a Scala-based analytics system (http://www.hiringthing.com), and I'm finding that I'm often asking myself the following question. Given a "pure" function with no side effects, if I hit that function twice with the same inputs, can I expect the compiler to reuse the value generated from the first run, or will it go through all the code again. Put another way, is the first example below more efficient than the second?
def add(x: Int, y: Int) = x + y * 10000000000
val a = add(1,2)
do_something(a)
do_another_thing(a)
vs.
def add(x: Int, y: Int) = x + y * 10000000000
do_something( add(1,2) )
do_another_thing( add(1,2) )
If the compiler can indeed optimize the second case, are there limits to the complexity of the function?
What I want to do is avoid running certain math heavy functions multiple times just for the sake of programming convenience...
Thanks