On Friday, August 15, 2014 2:21:05 PM UTC-4, Neal Becker wrote:
>
> I'm trying to do numerical integration.  I want a function that has state 
> information. 
>
> Let's say I'm trying to integrate some function F over x.  In addition, F 
> has 
> some state 
>
> function F (x, state) = <do something with x and state> 
>
 
As others have pointed out, the simplest solution here is a closure: 
integrate(x -> F(x,state), ...)

In python (and in c++), one way is to make F a class (which can have 
> state), and 
> overload the function call operator.  Then this can be passed to the 
> numerical 
> integrator. 
>

This solution always amazes me, considering that Python has closures (and 
now C++ has lambdas), and the closure solution is vastly simpler. 

(In my NLopt Python interface, I often field similar questions about how to 
pass extra state, so I feel like there is a cultural blindness in CS these 
days to closures, probably stemming from historical limitations of C++ etc.)

Reply via email to