I am trying to implement a generic timer function in OCaml which will take as input a function of arbitrary arity and return type 'r and returns a function with:
- the same arity and types of input parameters , and
- return type
float * 'r
where the float would be a metric of the time spent in the function (e.g. reported bySys.time()
)
The problem is I can't implement it in such a way that it can handle functions of any arity. E.g. the following code:
let timer f = let timerf x y = let t0 = Sys.time () in let result = f x y in let diff = Sys.time() -. t0 in diff, result in timerf
works only with functions of input arity 2. It is not obvious to me how to generalize it to handle functions of any arity. I was hoping the partial function applications would somehow magically solve the conundrum but I can't get it to work.