How can you define a function myEval(f, args)
in Scala which takes as input another function f
and arguments args
and whose output is f(args)
?
I don't want myEval
to have any prior knowledge about the arity or argument types of f
.
Why is this useful? It is one way to solve the problem of implementing a generic timeMyFunction(f, args)
method. If there's a way to do this by some sort of lazy val construction, that would also be interesting.
Edit: The better way to implement a timing method is explained in this question. By calling timeMyFunction( { f(args) } )
, the function call is wrapped in an anonymous function Unit => Unit
. So timeMyFunction
only needs to take 0-arity functions.
Edit 2: See Dirk's answer for what is perhaps a more efficient way which avoids an anonymous function by passing f
by reference.
So my justification for the question is now purely my Scala education.