Name: Anonymous 2011-04-01 2:00
Hey, /porg/, pretty much every language has a way to do this, but I'm not sure what it's called...
Suppose I write some useful algorithm, like a sort. And, I'd like to package up that code and make it available for other programs to use. But I can't just make it a "simple" function call, because I need the caller to supply a little bit of the code -- for example, he might need to provide the comparison function for the elements being sorted. You could consider the comparison function as just another argument to the function, but it's more than that. It's a sort of two-way exchange, where I write some of the code and the caller writes some of the code, and the two transfer execution back and forth between each other.
A lot of languages use callbacks or "anonymous" functions for this sort of thing. What are mechanisms that other languages provide? Are there any significant advantages?
Suppose I write some useful algorithm, like a sort. And, I'd like to package up that code and make it available for other programs to use. But I can't just make it a "simple" function call, because I need the caller to supply a little bit of the code -- for example, he might need to provide the comparison function for the elements being sorted. You could consider the comparison function as just another argument to the function, but it's more than that. It's a sort of two-way exchange, where I write some of the code and the caller writes some of the code, and the two transfer execution back and forth between each other.
A lot of languages use callbacks or "anonymous" functions for this sort of thing. What are mechanisms that other languages provide? Are there any significant advantages?