Category : functional-programming

As per the title, I’d like to understand how to make std::vector an Applicative but not a Monad (well, not yet). This is just for the sake of exploring and understanding Boost.Hana and functional programming. Functor In hana/ext/std/vector.hpp, the Functor instance of std::vector is commented out, however, it seems to work fine (compiler explorer) if ..

Read more

tl;dr Is the customization of boost::hana::transform for std::vector (via specializing boost::hana::tranform_impl for the tag ext::std::vector) a form of Adaptor pattern that wraps the STL’s std::transform into Hana’s interface function boost::hana::transform? Why this question I’m reading Dive into Design Patterns, and I’m a bit bothered by the fact that this resource, just like any others I’ve ..

Read more

everyone. I’m trying to write some code to implement a concurrent system via actor pattern. The actor’s interface is like: template <class SourceMessageType, class MessageType> class actor{ public: using value_type = MessageType; void process_message(SourceMessageType&& message); template <class EmitFunction> void on_message(EmitFunction emit); private: std::function<void(MessageType&&)> m_emit; }; In my program, there are two actors, one is Service ..

Read more

When I use the Stream Library (http://jscheiny.github.io/Streams/api.html#) I can do similar things like in Java-Streams: #include "Streams/source/Stream.h" #include <iostream> using namespace std; using namespace stream; using namespace stream::op; int main() { list<string> einkaufsliste = { "Bier", "Käse", "Wurst", "Salami", "Senf", "Sauerkraut" }; int c = MakeStream::from(einkaufsliste) | filter([] (string s) { return !s.substr(0,1).compare("S"); }) | ..

Read more

I have a piece of code where I process a std::vector by splitting it into groups and processing each group. An if–else determines the processing each group undergoes: groups taking the if branch will end up giving a single ouptut, groups taking the else branch give rise to several outputs. All these outputs are collected ..

Read more

Let’s say that given a range like this std::vector<int> v{1, 4, 7, 2}; I want to generate another range where all even number are repeated a number of times equal to their value, whereas all odd numbers are left unchanged. A possible solution is the following: #include <iostream> #include <range/v3/view/join.hpp> #include <range/v3/view/transform.hpp> #include <range/v3/view/repeat_n.hpp> #include ..

Read more

These are some inputs and relative desired outputs std::vector<std::string> v1{"aeiou", "bcdfghjk"}; std::vector<std::string> v2{"aeiou", "bcd"}; auto w1 = v1 | wanne_be_transpose; auto w2 = v2 | wanne_be_transpose; // w1 = {"ab","ec","id","of","ug","h","j","k"} // w2 = {"ab","ec","id","o","u"} Honestly I have no idea how to emulate it with ranges. (Even less in the general case of v1.size() > 2, ..

Read more

Given this code, #include <iostream> #include <range/v3/view/join.hpp> #include <range/v3/view/repeat.hpp> #include <range/v3/view/take.hpp> #include <range/v3/view/transform.hpp> #include <utility> namespace rv = ranges::views; int main() { std::vector<int> v{0,1,2,3}; std::cout << (v | rv::transform([](int x){ return rv::repeat(x) | rv::take(x); }) | rv::join) << std::endl; // outputs [1,2,2,3,3,3] } I’d suppose there’s function that does the job combining join and transform ..

Read more

I would like to curry a function that takes an abstract argument. This makes my compiler angry: #include <functional> class MyAbstractParentClass { public: virtual void someVirtualMethod() = 0; }; class MyConcreteChildClass: public MyAbstractParentClass { public: virtual void someVirtualMethod() override {} }; void myFunction(const MyAbstractParentClass& myAbstractObject) {} int main(int argc, const char * argv[]) { const ..

Read more

The type hana::optional represents an Optional value whose optional-ness is known at compile-time and it models, among others, the concept of Monad. Since it is a compile-time optional, the question of what happens at run-time if I try to hana::flatten a non-empty hana::optional which doesn’t wrap another hana::optional makes no sense, because because the error ..

Read more