When to use functors over lambdas

A lambda is a functor – just defined with a shorter syntax. The problem is that this syntax is limited. It doesn’t always allow you to solve a problem in the most efficient and flexible way – or at all. Until C++14, the operator() couldn’t even be a template. Furthermore, a lambda has exactly one … Read more

F# changes to OCaml [closed]

This question has been answered for some time now, but I was quite surprised that most of the answers say what OCaml features are missing in F# – this is definitely good to know if you want to port existing OCaml programs to F# (which is probably the motivation of most of the referenced articles). … Read more

Why does a js map on an array modify the original array?

You’re not modifying your original array. You’re modifying the objects in the array. If you want to avoid mutating the objects in your array, you can use Object.assign to create a new object with the original’s properties plus any changes you need: const freeProduct = function(products) { return products.map(x => { return Object.assign({}, x, { … Read more

How to use sort() in C++ with custom sort member function?

It is, but in general I would encourage just using a proper functor or a lambda: Using a lambda: std::sort(payloadIndices.begin(), payloadIndices.end(), [this](int a, int b){ return this->sortByWeights(a, b); }); Alternatively using std::mem_fn: auto sorter = std::bind(std::mem_fn(SimpleGreedyMappingTechnique::sortByWeights), this); std::sort(payloadIndices.begin(), payloadIndices.end(), sorter); Alternatively using a functor: namespace{ struct indicies_less_than { const SimpleGreedyMappingTechnique & mapping_tech; indicies_less_than(const SimpleGreedyMappingTechnique & … Read more

passing functor as function pointer

You cannot directly pass a pointer to a C++ functor object as a function pointer to C code (or even to C++ code). Additionally, to portably pass a callback to C code it needs to be at least declared as an extern “C” non-member function. At least, because some APIs require specific function call conventions … Read more