[Optional] Monadic bind

While playing around with boost::optionals I had code like the following struct A { ... int i; ... }; boost::optional a; // do something with 'a' // optional-projection to a::i boost::optional<int> a_i; if(a) a_i = a->i; which seemed quite verbose for something that's rather natural in functional languages: boost::optional is a monadic structure, which lifts a type T by extending it with a new bottom-element (represented by the empty optional<T>), thus it should also have a "bind" and a "map"-equivalent, which could be implemented for instance as as member functions of optional template<class B> auto bind(B&& binder) const -> boost::optional<typename std::decay<decltype(std::bind(binder, get())())>::type> { if(this->is_initialized()) { return std::bind(binder, get())(); } else { return boost::none; } } and template<class B> auto map(B&& f) const -> boost::optional<typename std::decay<decltype(std::bind(f, get())())>::type> { if(this->is_initialized()) { return std::bind(f, get())(); } else { return boost::none; } } where map lifts f from A->B to optional -> optional< B > and applies 'this' to it. This would allow things like boost::optional a; auto a_i = a.map(&A::i); // "optional-projection"; type of a_i is boost::optional<int> Tobias -- View this message in context: http://boost.2283326.n4.nabble.com/Optional-Monadic-bind-tp4677431.html Sent from the Boost - Dev mailing list archive at Nabble.com.

2015-06-22 15:49 GMT+02:00 Tobias Loew <Tobias.Loew@steag.com>:
Could you give us an example here that would compile? boost::optional a; The above is not a valid variable declaration (template argument missing), so it is difficult for me to gather what you are trying to do. Regards, &rzej

Sorry for that, I've made in implementation with free functions which compiles on VS 2012: #include <assert.h> #include <boost/optional.hpp> template<class T, class B> auto bind(const boost::optional<T>& optional, B&& binder) -> typename std::decay<decltype(std::bind(binder, *optional)())>::type { if(optional) { return std::bind(binder, *optional)(); } else { return boost::none; } } template<class T, class B> auto map(const boost::optional<T>& optional, B&& binder) -> boost::optional<typename std::decay<decltype(std::bind(binder, *optional)())>::type> { if(optional) { return std::bind(binder, *optional)(); } else { return boost::none; } } void foo() { struct A { int i; }; boost::optional<A> opt_a; auto opt_i = map(opt_a, &A::i); assert(!opt_i); A a = {42}; opt_a = a; auto opt_i2 = map(opt_a, &A::i); assert(*opt_i2 == 42); } Von: Andrzej Krzemienski [via Boost] [mailto:ml-node+s2283326n4677432h98@n4.nabble.com] Gesendet: Montag, 22. Juni 2015 16:02 An: Löw, Tobias (STEAG Energy Services GmbH) Betreff: Re: [Optional] Monadic bind 2015-06-22 15:49 GMT+02:00 Tobias Loew <[hidden email]</user/SendEmail.jtp?type=node&node=4677432&i=0>>:
Could you give us an example here that would compile? boost::optional a; The above is not a valid variable declaration (template argument missing), so it is difficult for me to gather what you are trying to do. Regards, &rzej _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost ________________________________ If you reply to this email, your message will be added to the discussion below: http://boost.2283326.n4.nabble.com/Optional-Monadic-bind-tp4677431p4677432.h... To unsubscribe from [Optional] Monadic bind, click here<http://boost.2283326.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4677431&code=VG9iaWFzLkxvZXdAc3RlYWcuY29tfDQ2Nzc0MzF8MTI1MDAzMDE5Mg==>. NAML<http://boost.2283326.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> -- View this message in context: http://boost.2283326.n4.nabble.com/Optional-Monadic-bind-tp4677431p4677434.h... Sent from the Boost - Dev mailing list archive at Nabble.com.

2015-06-22 16:17 GMT+02:00 Tobias Loew <Tobias.Loew@steag.com>:
Would you find the following an acceptable solution? ``` #include <assert.h> #include <boost/bind.hpp> #include <boost/optional.hpp> template <typename T, typename F> auto map2(const boost::optional<T>& o, F f) -> boost::optional<decltype(f(*o))> { if (o) return f(*o); else return boost::none; } void foo() { struct A { int i; }; boost::optional<A> opt_a; auto opt_i = map2(opt_a, boost::bind(&A::i, _1)); assert(!opt_i); A a = {42}; opt_a = a; auto opt_i2 = map2(opt_a, boost::bind(&A::i, _1)); assert(opt_i2); assert(*opt_i2 == 42); } int main() { foo(); } ``` It requires of you to type a bit more, but at the same time it is more flexible: you can type any free function on T, including a lambda. Regards, &rzej

I deliberately used this std::bind syntax since it accepts any callable (including things like pointer-to-member [functions]). I tried lambdas with my code and they just work. E.g. auto opt_i3 = map(opt_a, [](const A& a){ return a.i;}); assert(*opt_i3 == 42); Furthermore without the std::decay boost::optional<decltype(f(*o))> will be a boost::optional<T const&>. Tobias Von: Andrzej Krzemienski [via Boost] [mailto:ml-node+s2283326n4677436h22@n4.nabble.com] Gesendet: Montag, 22. Juni 2015 16:44 An: Löw, Tobias (STEAG Energy Services GmbH) Betreff: Re: [Optional] Monadic bind 2015-06-22 16:17 GMT+02:00 Tobias Loew <[hidden email]</user/SendEmail.jtp?type=node&node=4677436&i=0>>:
Would you find the following an acceptable solution? ``` #include <assert.h> #include <boost/bind.hpp> #include <boost/optional.hpp> template <typename T, typename F> auto map2(const boost::optional<T>& o, F f) -> boost::optional<decltype(f(*o))> { if (o) return f(*o); else return boost::none; } void foo() { struct A { int i; }; boost::optional<A> opt_a; auto opt_i = map2(opt_a, boost::bind(&A::i, _1)); assert(!opt_i); A a = {42}; opt_a = a; auto opt_i2 = map2(opt_a, boost::bind(&A::i, _1)); assert(opt_i2); assert(*opt_i2 == 42); } int main() { foo(); } ``` It requires of you to type a bit more, but at the same time it is more flexible: you can type any free function on T, including a lambda. Regards, &rzej _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost ________________________________ If you reply to this email, your message will be added to the discussion below: http://boost.2283326.n4.nabble.com/Optional-Monadic-bind-tp4677431p4677436.h... To unsubscribe from [Optional] Monadic bind, click here<http://boost.2283326.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4677431&code=VG9iaWFzLkxvZXdAc3RlYWcuY29tfDQ2Nzc0MzF8MTI1MDAzMDE5Mg==>. NAML<http://boost.2283326.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> -- View this message in context: http://boost.2283326.n4.nabble.com/Optional-Monadic-bind-tp4677431p4677437.h... Sent from the Boost - Dev mailing list archive at Nabble.com.

Von: Andrzej Krzemienski [via Boost] [mailto:ml-node+s2283326n4677451h87@n4.nabble.com] Gesendet: Montag, 22. Juni 2015 20:54 An: Löw, Tobias (STEAG Energy Services GmbH) Betreff: Re: [Optional] Monadic bind 2015-06-22 16:59 GMT+02:00 Tobias Loew <[hidden email]</user/SendEmail.jtp?type=node&node=4677451&i=0>>:
Interesting. Is it implementable in C++03? I managed to change your map2 to compile in C++03 (on VS 2008) template <typename T, typename F> boost::optional< typename boost::remove_cv< typename boost::remove_reference< typename boost::result_of<F(T)>::type >::type >::type > map2(const boost::optional<T>& o, F f) { if (o) return f(*o); else return boost::none; } I didn’t find an easy way to make my map compile in C++03. Technically, it should be possible by using the same tricks that boost::bind itself uses, but I think that would be by far too much for such a feature. A good solution could be: using a map2-like version (only accepting functions/functors) for C++03 compilers and a the more general version (accepting arbitrary callables) for C++11 compilers. -- View this message in context: http://boost.2283326.n4.nabble.com/Optional-Monadic-bind-tp4677431p4677464.h... Sent from the Boost - Dev mailing list archive at Nabble.com.
participants (2)
-
Andrzej Krzemienski
-
Tobias Loew