Thanks, I will take a look at functors when I upgrade to 0.4. I'm not a very sophisticated programmer (no functional programming experience). I looked up but didn't understand the description of monads at Wikipedia.
It seems like this feature isn't something that most people on this list want or need, so maybe I'm suffering from "premature closure" on the problem I'm actually interested in. The scenario I have in mind is an objective function to be used in a black-box optimization (no information available other than the objective function value). The objective function needs to do a lot of expensive set-up computations when it is first called, but can re-use this same data on the second and subsequent calls. Is having the objective function save its state between calls not the right approach here? On Tuesday, February 3, 2015 at 4:26:40 PM UTC-8, Steven G. Johnson wrote: > > On Tuesday, February 3, 2015 at 1:11:36 PM UTC-5, Peter Simon wrote: >> >> I saw a suggestion from Miles Lubin in >> https://groups.google.com/forum/#!topic/julia-opt/z8Ld4-kdvCI for >> avoiding redundant calculations that appeared to involve saving state >> between function calls, and I wonder what is the standard Julian way to do >> this. I'm asking here because this seems to be a general Julia question. >> In Matlab I would use a "persistent" variable. What is the corresponding >> idiom in Julia? Closures? Or something else? >> > > I suppose you could simulate this with a functor object. That is, you > define a type that contains fields for the state you need to save, and > overload call(::MyType, args...) so that you can call your type as a > function. (This is a new feature in Julia 0.4.) > > (You can probably also use monads if you want to go all functional...) >
