2 ms·
While you can take the address of cube, you can't do this trick more than one level deep: you can't return a new function from "deriv".
by pjc50 8y ago
While you can take the address of cube, you can't do this trick more than one level deep: you can't return a new function from "deriv".
- dmitris 8y agoHere's a deriv example in Go: https://play.golang.org/p/kUM5qn7oiXg https://play.golang.org/p/kUM5qn7oiXg I find that it follows the functional spec quite closely, thanks to Go functions being first class citizens.
- jcelerier 8y agoHere's the generic C++ version (https://gcc.godbolt.org/z/mMln6L https://gcc.godbolt.org/z/mMln6L): constexpr double delta = 1.0e-6; constexpr auto deriv = [] (auto f) { return [=] (double x) { return (f(x+delta) - f(x)) / delta; }; }; constexpr auto f¨ = [] (auto f) { return deriv(deriv(f)); }; double res = f¨([] (double x) { return x*x*x; })(2.0);