Serving the Quantitative Finance Community

 
User avatar
Cuchulainn
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ quiz --- generic programming

April 26th, 2013, 5:44 am

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!
Last edited by Cuchulainn on April 25th, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ quiz --- generic programming

November 29th, 2014, 3:56 pm

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.
 
User avatar
pcaspers
Posts: 30
Joined: June 6th, 2005, 9:49 am
Location: Germany

C++ quiz --- generic programming

November 29th, 2014, 5:55 pm

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;}
 
User avatar
lballabio
Topic Author
Posts: 0
Joined: January 19th, 2004, 12:34 pm

C++ quiz --- generic programming

November 29th, 2014, 7:21 pm

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.)
Last edited by lballabio on November 28th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
pcaspers
Posts: 30
Joined: June 6th, 2005, 9:49 am
Location: Germany

C++ quiz --- generic programming

November 30th, 2014, 7:17 am

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;}
 
User avatar
Cuchulainn
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ quiz --- generic programming

November 30th, 2014, 11:59 am

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
Last edited by Cuchulainn on November 29th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ quiz --- generic programming

November 30th, 2014, 12:20 pm

Some syntactical sugaring
Last edited by Cuchulainn on November 29th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
lballabio
Topic Author
Posts: 0
Joined: January 19th, 2004, 12:34 pm

C++ quiz --- generic programming

November 30th, 2014, 3:39 pm

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.
 
User avatar
Cuchulainn
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ quiz --- generic programming

November 30th, 2014, 4:31 pm

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.
Last edited by Cuchulainn on November 29th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ quiz --- generic programming

November 30th, 2014, 6:27 pm

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)?
Last edited by Cuchulainn on November 29th, 2014, 11:00 pm, edited 1 time in total.