Serving the Quantitative Finance Community

  • 1
  • 3
  • 4
  • 5
  • 6
  • 7
  • 10
 
User avatar
Cuchulainn
Posts: 22927
Joined: July 16th, 2004, 7:38 am

C++ quiz --- generic programming

November 30th, 2014, 6:43 pm

QuoteOriginally posted by: pcaspersthen 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;}I would like to say g = f - y; g(2) etc.i.e . "functions as first-class values".
Last edited by Cuchulainn on November 29th, 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 pm

yap. I tried to write something relying on actual functions (due to outrun's complaint). My understanding was you havef: a -> band want to implementh: (a -> b) -> a -> bsuch that h(f) : a -> b, [h(f)](x) = f(x) -y with a fixed y.What I wrote is ratherh*: ((a -> b), a) -> bthe uncurry'ed version of h.
 
User avatar
Cuchulainn
Posts: 22927
Joined: July 16th, 2004, 7:38 am

C++ quiz --- generic programming

November 30th, 2014, 7:38 pm

Quoteold school way does not support higher-order functions.
 
User avatar
Cuchulainn
Posts: 22927
Joined: July 16th, 2004, 7:38 am

C++ quiz --- generic programming

November 30th, 2014, 8:12 pm

I want this
Last edited by Cuchulainn on November 29th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 22927
Joined: July 16th, 2004, 7:38 am

C++ quiz --- generic programming

November 30th, 2014, 9:07 pm

QuoteOriginally posted by: outrunThe drawback of this "composition" signature is you implement is that way, that it needs to returns a pointer to an function object, .. and who's going to clean up that object afterwards?I would go into this direction stackoverflow: Pass lambda expression to lambda argument c++11..I think I would pick the last option: "Alternatively, you can drop the syntactic sugar and roll the first lambda equivalent manually .. or wait for C++14's polymorphic lambdas"Even better, it won't even compile.I agree. A hybrid approach below would be if function pointers are a must.(BTW the real Q is: can we write a function in C++ that _return_ an function. This question is unambiguous).
Last edited by Cuchulainn on November 29th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 22927
Joined: July 16th, 2004, 7:38 am

C++ quiz --- generic programming

December 1st, 2014, 3:24 am

QuoteOriginally posted by: lballabioQuoteOriginally 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.auto (and std::function) walk like a function, talks like a function, kwaks like a function. So it is a function...yes?... or is there some conspiracy here QuoteClass template std::function is a general-purpose polymorphic function wrapper. Instances of std::function can store, copy, and invoke any Callable target -- functions, lambda expressions, bind expressions, or other function objects, as well as pointers to member functions and pointers to data members. .NET delegates are built on a similar principle.
Last edited by Cuchulainn on November 30th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 22927
Joined: July 16th, 2004, 7:38 am

C++ quiz --- generic programming

December 1st, 2014, 9:05 am

Quotewhen you say "walk like a function, talks like a function, kwaks like a function" you mean that they are callable? yes. QuoteIt basically creates a struct with an operator(), and the name of that struct is some unique type name that's hidden (anonymous). If you capture it then you'll get a object of that type. Those types are also convertable to std::function outof the box.I know. So?
Last edited by Cuchulainn on November 30th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 22927
Joined: July 16th, 2004, 7:38 am

C++ quiz --- generic programming

December 1st, 2014, 11:19 am

Indeed, I know. Two different things won't have the same properties..I want to do mathematics in C++, i.e. vector space of real-valued functions of a real variable over the field of real numbers. That's as precise as you get.Function composition f(g(x)) is not part of vector space axiom. How it is done is an implementation issue. I get the impression that it cannot be done - at least not directly - in C++ 11.So, has anyone it? Quoteyou need to be precise in stating what you want,Why?Well, sometimes you are working on something and you want some feedback as @Luigi and @pcaspers have immediately done with some code hints. Especially when it is new stuff like now.
Last edited by Cuchulainn on November 30th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 22927
Joined: July 16th, 2004, 7:38 am

C++ quiz --- generic programming

December 1st, 2014, 2:37 pm

Actually, I don't know the (complete) answer at the time of writing. Maybe I can think longer but then there would be no need to post...And maybe someone has a much better solution.
Last edited by Cuchulainn on November 30th, 2014, 11:00 pm, edited 1 time in total.