Page 4 of 10

C++ quiz --- generic programming

Posted: April 26th, 2013, 5:44 am
by Cuchulainn
QuoteOriginally posted by: outrunQuoteOriginally posted by: CuchulainnQuoteOriginally posted by: outrunQuoteOriginally posted by: CuchulainnQuoteOriginally posted by: outrunWhat about parallel multicast?Some languages have a "For each" construct without any specific order guarantees, ideal for parallel execution.PLINQExactly!Any similar devs in C++ (i reckon on top of STL and Boost), in the vein of boost::multiindex?A priority queue (or set) of functors that get's fed to a threadpool would be cool. I think threadpool would be the workhorse?TPL - for example - uses a threadpool by default so low-level thread management can be replaced by more friendly tasks. Race conditions are still a (gratis) feature, however!

C++ quiz --- generic programming

Posted: November 29th, 2014, 3:56 pm
by Cuchulainn
I have a function f(x) (x variable) and a fixed number y.How do create a _function_ in C++ g(x) s.t.g(x) = f(x) - y for all x.// This is a higher-order functionWant g(1)g(0.5)etc.

C++ quiz --- generic programming

Posted: November 29th, 2014, 5:55 pm
by pcaspers
that's probably not what you meant, is it ... :-) #include <ql/quantlib.hpp>struct f { typedef double argument_type; typedef double result_type; result_type operator()(const argument_type x) const { return x * x; }} f_;struct minus_const { static constexpr double y = 1.0; typedef double argument_type; typedef double result_type; result_type operator()(const argument_type x) const { return x - y; }} minus_const_;int main() { QuantLib::composed_function<minus_const,f> g(minus_const_, f_); std::cout << g(2.0) << std::endl;}

C++ quiz --- generic programming

Posted: November 29th, 2014, 7:21 pm
by lballabio
I don't have much experience with C++14 yet, but I think that would beauto g = [f,y](auto x) { return f(x) - y; };(disclaimer; I haven't tried it.)

C++ quiz --- generic programming

Posted: November 30th, 2014, 7:17 am
by pcaspers
then the old school way ?const double y = 1.0;double f(const double x) { return x*x;}double g(double (*f)(double), double x) { return (*f)(x) - y;}int main() { std::cout << g(&f,2.0) << std::endl;}

C++ quiz --- generic programming

Posted: November 30th, 2014, 11:59 am
by Cuchulainn
QuoteOriginally posted by: lballabioI don't have much experience with C++14 yet, but I think that would beauto g = [f,y](auto x) { return f(x) - y; };(disclaimer; I haven't tried it.)In bocca al lupo! grazie, Maestro

C++ quiz --- generic programming

Posted: November 30th, 2014, 12:20 pm
by Cuchulainn
Some syntactical sugaring

C++ quiz --- generic programming

Posted: November 30th, 2014, 3:39 pm
by lballabio
QuoteOriginally posted by: outrunFor the sake of good understanding and people not learning wrong things from a technical quiz: The captured variables *are* function objects!Correct. the lambda is actually an instance of some kind of anonymous class, internally defined by the constructor, that stores the captured variables as data members.

C++ quiz --- generic programming

Posted: November 30th, 2014, 4:31 pm
by Cuchulainn
Yeah, I have just built a vector space for functions as well as an algebra Now I can do things like this as a higher order function.

C++ quiz --- generic programming

Posted: November 30th, 2014, 6:27 pm
by Cuchulainn
QuoteOriginally posted by: lballabioI don't have much experience with C++14 yet, but I think that would beauto g = [f,y](auto x) { return f(x) - y; };(disclaimer; I haven't tried it.) this (as well)?