I have a Spline (interpolation) type:
type
Spline*[T] = ref object
x: seq[float]
coeffs: seq[tuple[float, float, T, T]]
And I can evaluate it at a point t using:
eval*[T](spline: Spline[T], t: float): T
What I would like to do, is to create a proc for a specific spline so that I can call it without having to supply the Spline. Something like this:
var spline = newSpline(x, y)
# Current way to evaluate it:
echo spline.eval(2.5)
# How I would like it:
var f = Spline2Proc(spline) # this is just a placeholder for the magic I want.
echo f(2.5)
The reason for this is that I want to be able to pass a Spline as a proc to for example a proc that does numerical integration of a function.
Is this possible to do in Nim (without too much troubles)?
proc Spline2Proc[T](spline: Spline[T]): proc(t: float): T =
(proc(t: float): T = eval(spline, t))
Aaahhhh makes total sense :) this is what's called a closure?
Thank you very much @LeuGim :-D