Inny wrote:This will run the function every time it's called with the arguments from the first call. This doesn't "only run the function once".
Well, that was intended. Moses was kind of inspired by Underscore.js library, at first, where _.once is implemented as a function for lazy initialization. Hence the actual Lua implementation. I do admit the name _.once is a bit misleading, though.
In the version you linked, _.once is a partial application of the function _.before with "times" equal to 2
The actual function is only executed once, when times == 1, and then the reference is nulled so that the function could be garbage collected. It never executed the function again, and only the return value is retained permanently. In your case, yeah, __.once is misnamed. I'm not sure what a proper rename would be, since yours appears to be a partial application with all parameters filled. I don't think such a thing has a name.
Inny wrote:In your case, yeah, __.once is misnamed. I'm not sure what a proper rename would be, since yours appears to be a partial application with all parameters filled. I don't think such a thing has a name.
That is perfectly right.
I'll fix that as soon as possible
If you guys are talking about literally. Only do the function once per game startup, then isn't a coroutine a candidate? literally taking a function as a parameter and running it, but once run the coroutine is dead. (Sorry if I'm misunderstanding).