Concerning free software, programming, and whatever else I find interesting. Also the blog for my Web site, frigidcode.com.
C++ had lambda functions!
Posted 01-31-2013 at 01:21 AM by hydraMax
Tags c++, programming
I'm doing a CS class which forces me to use C++. I was not very excited about this. However, I have recently be greatly encouraged by the discovery that C++ does, in fact, have lambda functions (or, more properly, anonymous functions) at least in the C++11 standard:
https://en.wikipedia.org/wiki/Anonym...ctions#C.2B.2B
Basically, the big deal here is that I can construct new functions /inside/ of other functions, and even capture variables from the context. This, in turn, means that I can pass in customized or constructed functions into other functions, and thus program in a more functional programming style.
So, for example, instead of creating a function that reads lines and parses them, I can create a function that reads lines, and receives (as a parameter) a function that parses strings. This means that (1) I can keep the two functionality concepts distinct in the code (reading lines and parsing strings) and (2) the code can be easily re-factored, because I can replace the parsing function with another function that parses different, or I can reuse the parsing function as an argument to another function that retrieves strings in a different way.
I suppose I could do something similar with classes; but then I would have to muck around with setting up a sensible inheritance tree, and that does not appeal to me in the slightest.
https://en.wikipedia.org/wiki/Anonym...ctions#C.2B.2B
Basically, the big deal here is that I can construct new functions /inside/ of other functions, and even capture variables from the context. This, in turn, means that I can pass in customized or constructed functions into other functions, and thus program in a more functional programming style.
So, for example, instead of creating a function that reads lines and parses them, I can create a function that reads lines, and receives (as a parameter) a function that parses strings. This means that (1) I can keep the two functionality concepts distinct in the code (reading lines and parsing strings) and (2) the code can be easily re-factored, because I can replace the parsing function with another function that parses different, or I can reuse the parsing function as an argument to another function that retrieves strings in a different way.
I suppose I could do something similar with classes; but then I would have to muck around with setting up a sensible inheritance tree, and that does not appeal to me in the slightest.
Total Comments 0