• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Louis Dionne 2013-2017
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
4 
5 #include <boost/hana/assert.hpp>
6 #include <boost/hana/chain.hpp>
7 #include <boost/hana/eval.hpp>
8 #include <boost/hana/lazy.hpp>
9 
10 #include <functional>
11 #include <iostream>
12 #include <sstream>
13 namespace hana = boost::hana;
14 
15 
16 template <typename T>
read_(std::istream & stream)17 T read_(std::istream& stream) {
18     T x;
19     stream >> x;
20     std::cout << "read " << x << " from the stream\n";
21     return x;
22 }
23 
main()24 int main() {
25     std::stringstream ss;
26     int in = 123;
27 
28     std::cout << "creating the monadic chain...\n";
29     auto out = hana::make_lazy(read_<int>)(std::ref(ss))
30         | [](auto x) {
31             std::cout << "performing x + 1...\n";
32             return hana::make_lazy(x + 1);
33         }
34         | [](auto x) {
35             std::cout << "performing x / 2...\n";
36             return hana::make_lazy(x / 2);
37         };
38 
39     std::cout << "putting " << in << " in the stream...\n";
40     ss << in; // nothing is evaluated yet
41 
42     std::cout << "evaluating the monadic chain...\n";
43     auto eout = hana::eval(out);
44 
45     std::cout << "the result of the monadic chain is " << eout << "\n";
46     BOOST_HANA_RUNTIME_CHECK(eout == (in + 1) / 2);
47 }
48