tag:blogger.com,1999:blog-8638474063464489651.post7156329425815634732..comments2024-01-15T11:38:29.051-08:00Comments on Multi-paradigm: Partial Application in C++Splinter of Chaoshttp://www.blogger.com/profile/14715348728512729776noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-8638474063464489651.post-6564123594218394422013-01-23T06:59:10.232-08:002013-01-23T06:59:10.232-08:00I'm not familiar with Pheonix, but in terms of...I'm not familiar with Pheonix, but in terms of mimicking Haskell... <br /><br />A couple months after writing this article, I thought more about currying and function objects. I've been meaning to rewrite this article. Really mimicking Haskell would mean you could do something like this:<br /><br />auto plusOne = std::plus< int >()(1); <br /><br />Which is, of course, impossible. But you reminded me of something I had been procrastinating on: https://gist.github.com/4606174<br /><br />Back to your original question, it sounds like a fine idea. As I said, I'm not familiar with the library, which is a result of my own ignorance, but I hear about it a lot (seems popular) and I can see it offers a flexibility my solution doesn't (re-ordering arguments, applying the last argument, etc.) similar to std::bind. I think I've read that it's great for operator overloading, too.<br /><br />Not sure how I could better answer your question, but good luck.Splinter of Chaoshttps://www.blogger.com/profile/14715348728512729776noreply@blogger.comtag:blogger.com,1999:blog-8638474063464489651.post-79657276911691740842013-01-22T07:21:46.022-08:002013-01-22T07:21:46.022-08:00Thank for sharing your ideas! I also have interest...Thank for sharing your ideas! I also have interest using C++ in functional style. And there is a very good boost library called 'phoenix'. Let's try to implement partial application using 'phoenix':<br /><br />#include <br />#include <br />#include <br />#include <br /><br />namespace fs = boost::phoenix;<br /><br />BOOST_PHOENIX_ADAPT_CALLABLE(plus, std::plus, 2);<br /><br />int main()<br />{<br /> using fs::arg_names::arg1;<br /> <br /> auto plusone = plus(arg1, 1); <br /> std::cout << plusone(2) << std::endl;<br /><br /> return 0;<br />}<br /><br />That's all. :-) So, my question is: could be better to implement a thin wrapper around 'phoenix' library in order to mimic Haskell functionality?<br /><br />sigidagihttps://www.blogger.com/profile/00206229569115264837noreply@blogger.comtag:blogger.com,1999:blog-8638474063464489651.post-17275691531113615642012-09-07T12:14:57.531-07:002012-09-07T12:14:57.531-07:00I do appreciate constructive criticism, but if I r...I do appreciate constructive criticism, but if I recognized my own faults, I would have already corrected them. Could you be more specific? <br /><br />I did realize that i had forgotten the ouput iterator in the call to std::transform. Also, making operator* a move operation was wrong.<br /><br />As for how to make the code less noisy or painful: I do not often get a chance to hear what anyone has to say about my code. If you suggest an improvement, I will listen.Splinter of Chaoshttps://www.blogger.com/profile/14715348728512729776noreply@blogger.comtag:blogger.com,1999:blog-8638474063464489651.post-74240714771500305822012-09-07T06:35:15.351-07:002012-09-07T06:35:15.351-07:00Lots of typos, reading your code is very noisy and...Lots of typos, reading your code is very noisy and painful. Can you fix syntax errors?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8638474063464489651.post-89976602286309561822012-09-06T09:56:05.244-07:002012-09-06T09:56:05.244-07:00For a lone coder:
There are no rules. You can do w...For a lone coder:<br />There are no rules. You can do whatever you want. Personally, I'm working on my own Haskell-like library because, while I'm not a huge fan of Haskell, it scratches the right itch. As a hobbyist, my highest freedom is no one being able to tell me not to use my own library that does what i want it to do. For the consumer of my software, it only matters that it compiles or executes.<br /><br />BTW: I do not consider C++ a non-functional language. It is a multi-paradigm language, agnostic of FP.<br /><br />For a team:<br />...or a larger matter: the C++ community at-large. C++ is a very formal language with various ideas of "good form" and "bad practise" in the back of one's mind. That which we see causes disorder or chaos we consider "bad form" and ban it. So it's really important that everyone on a project agree that partial application is good form. Is it?<br /><br />An existing practise is to create a function object that holds some data which it recalls when you call it. A comparator predicate object, for use in std::find_if, might hold a parameter that equals the value searched for. <br /><br />A generalization of that is std::bind, which could take your comparator and one argument (plus one std::placeholders::_n), and be passed to std::find_if. Instead of having to write a new struct for each comparison, one could write a normal function and wrap it in a bind. <br /><br />The closed and closure i defined can be thought of as specializations of std::bind. They are not foreign practises inserted into the language, but rather a less generic way of doing what's in the standard. Furthermore, they offer a more convenient syntax and can be used to write more generic functions than std::bind. (A higher-order-function using std::bind must know the function's arity. One using closure has no need.) <br /><br />I would like to see more use of functions like this, not because they are conventional, but because I would like to see them become conventional. <br /><br />Not just partial application, but composition (next article) and a generic fmap (future article). I'm on a kick right now for FP in C++. A lot of this stuff has only just recently become possible to do in C++ because of the inclusion of decltype, declval, perfect forwarding, rvalue-references, and all those other things that make people complain that C++ is too complex.Splinter of Chaoshttps://www.blogger.com/profile/14715348728512729776noreply@blogger.comtag:blogger.com,1999:blog-8638474063464489651.post-20625809234811664812012-09-05T17:24:59.840-07:002012-09-05T17:24:59.840-07:00of course you can do it. the real question is: is ...of course you can do it. the real question is: is it really worth it, for a lone coder, or for a team of typical game programmers? i'd love to hear your thoughts on this, as someone who toys around with forcing functional programming into non-functional languages.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8638474063464489651.post-44346771988696580042012-09-03T16:25:25.600-07:002012-09-03T16:25:25.600-07:00Down VoteDown VoteAnonymousnoreply@blogger.com