C ++のモナドとdo記法

ご存じのように、Haskellにはモナドがありますが、C ++にはありません。 しかし、C ++でのモナドの実装を妨げるものは何もありません。 実装に必要なものがすべて揃っているかどうかを見てみましょう。



Haskellのモナドは次のように定義されます。

class Monad m where (>>= ) ::ma -> (a->mb)->mb (>> ) ::m a->m b->mb return ::a->ma fail::String->ma
      
      





最初に、C ++で型を持っている場合でも、いくつかの抽象モナドを見てみましょう
 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a) , :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.
































Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a) , mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.
































Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a) , , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.
































Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m , :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.
































Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.
































Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m , m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.
































Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g) , , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.
































Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























 Monad. ,     . 
      

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.




























Monad. , .

template<typename A, typename B> Monad<B> operator>>=(Monad<A> ma, function<Monad<B> (A)> f) { ... // }




ma, f f.



template<typename A, typename B> Monad<B> operator>>(Monad<A> ma, Monad<B> mb) { return Ma >>= [=](A){ return mb; }; }



>>= , . ( m >> k = m >>= \_ -> k



), Haskell C++.



template<typename A> Monad <A> mreturn(A a) { ... // }



return C++, mreturn. , a



.

fail



, .



, C++, - . - Maybe



, ., std::future



. C++11. future T



, . , , - , , , , . future



get



.

template<class T> class future { public: ... T get(); // get ... };





C++11 – std::async



. , std::future



. : future<int> result = async(f); // - f int a = result.get(); // f .





mreturn



>>=



std::future



#include <future> using namespace std; template<typename A> future<A> mreturn(const A& a) { return async([=]{ return a; }); }



, future , . , .



template<typename A, typename B> future<B> operator>>=(future<A>& ma, const function<future<B> (A)>& f) { return async([&] () -> B { return f(ma.get()).get(); }); }



, , ma, f, f.



, . 3 , : Left identity : return a >>= f == fa Right identity: m >>= return == m Associativity: (m >>= f) >>= g == m >>= (\x -> fx >>= g)





C++, :

(mreturn(a) >>= f) == f(a)



, :

async([&] () -> B { return f(mreturn(a).get()).get(); }) == f(a)



, mreturn(a).get()



,



. a



std::future



, . , :

async([&] () -> B { return f(a).get(); }) == f(a)



, , f



, . , f



, ( ).



: m >>= mreturn == m



, :

async([&] () -> B { return mreturn(m.get()).get(); }) == m



mreturn(x).get()



, :

async([&] () -> B { return m.get(); }) == m



, m. , m.



: (m >>= f) >>= g == m >>= (\x -> fx >>= g)



, , . m f, , m. g, f. f g m.



. - : function<future<int> (int)> f = [](int a){ cout << a << '\n'; return mreturn(a + 6); }; int a = (mreturn(5) >>= f).get(); cout << a;



, 5 11.



C++11 mreturn >>=. : n3721

mreturn



make_ready_future



.

>>=



, std::future



then



. , A



, future. , , future , then



, .

unwrap



future<future> future. join



Control.Monad



.



then



: future<int> f(shared_ptr<stream> str) { shared_ptr<vector<char>> buf = ...; return str->read(512, buf).then([](future<int> op) { return op.get() + 11; }); } future<void> g() { shared_ptr<stream> s = ...; return f(s).then([s](future<int> op) { s->close(); }); }



, , . resumable : n3722 . : future<int> f(stream str) resumable { shared_ptr<vector<char>> buf = ...; int count = await str.read(512, buf); return count + 11; } future<void> g() resumable { stream s = ...; int pls11 = await f(s); s.close(); }



resumable



, , , . await



– ( <-



) do



. , str.read



f



future, await



int



, .



? , , : resumable



, , Fibers Windows boost::coroutine



. , . resumable



- , await



. , await



then



, await



. resumable



std::future



, then



get



. C++, , , , . std::future



.

, resumable



. , resumable



.































All Articles