Horizon
for_each.hpp
Go to the documentation of this file.
1 // Range v3 library
3 //
4 // Copyright Eric Niebler 2014-present
5 //
6 // Use, modification and distribution is subject to the
7 // Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // Project home: https://github.com/ericniebler/range-v3
12 //
13 
14 #ifndef RANGES_V3_VIEW_FOR_EACH_HPP
15 #define RANGES_V3_VIEW_FOR_EACH_HPP
16 
17 #include <utility>
18 
19 #include <meta/meta.hpp>
20 
21 #include <range/v3/range_fwd.hpp>
22 
25 #include <range/v3/utility/static_const.hpp>
26 #include <range/v3/view/all.hpp>
28 #include <range/v3/view/join.hpp>
30 #include <range/v3/view/single.hpp>
32 #include <range/v3/view/view.hpp>
33 
34 #include <range/v3/detail/prologue.hpp>
35 
36 namespace ranges
37 {
40 
41  namespace views
42  {
47  {
48  template(typename Rng, typename Fun)(
49  requires viewable_range<Rng> AND transformable_range<Rng, Fun> AND
50  joinable_range<transform_view<all_t<Rng>, Fun>>)
51  constexpr auto operator()(Rng && rng, Fun fun) const
52  {
53  return join(transform(static_cast<Rng &&>(rng), std::move(fun)));
54  }
55  };
56 
58  {
59  using for_each_base_fn::operator();
60 
61  template<typename Fun>
62  constexpr auto operator()(Fun fun) const
63  {
64  return make_view_closure(bind_back(for_each_base_fn{}, std::move(fun)));
65  }
66  };
67 
70  } // namespace views
71 
72  struct yield_fn
73  {
74  template(typename V)(
75  requires copy_constructible<V>)
76  single_view<V> operator()(V v) const
77  {
78  return views::single(std::move(v));
79  }
80  };
81 
84 
86  {
87  template(typename Rng)(
88  requires view_<Rng>)
89  Rng operator()(Rng rng) const
90  {
91  return rng;
92  }
93  };
94 
97 
98  struct yield_if_fn
99  {
100  template<typename V>
101  repeat_n_view<V> operator()(bool b, V v) const
102  {
103  return views::repeat_n(std::move(v), b ? 1 : 0);
104  }
105  };
106 
109 
111  {
112  template(typename F)(
113  requires invocable<F &>)
114  generate_n_view<F> operator()(bool b, F f) const
115  {
116  return views::generate_n(std::move(f), b ? 1 : 0);
117  }
118  };
119 
123 
125  template(typename Rng, typename Fun)(
126  requires viewable_range<Rng> AND views::transformable_range<Rng, Fun> AND
127  input_range<invoke_result_t<Fun &, range_reference_t<Rng>>>)
128  auto
129  operator>>=(Rng && rng, Fun fun)
130  {
131  return views::for_each(static_cast<Rng &&>(rng), std::move(fun));
132  }
134 } // namespace ranges
135 
136 #include <range/v3/detail/epilogue.hpp>
137 
138 #endif
CPP_concept input_range
\concept input_range
Definition: concepts.hpp:103
RANGES_INLINE_VARIABLE(detail::to_container_fn< detail::from_range< std::vector >>, to_vector) template< template< typename... > class ContT > auto to(RANGES_HIDDEN_DETAIL(detail
For initializing a container of the specified type with the elements of an Range.
Definition: conversion.hpp:399
defer< bind_back, Fn, Ts... > bind_back
Definition: meta.hpp:994
repeat_n_c< N::type::value, T > repeat_n
Generate list<T,T,T...T> of size N arguments.
Definition: meta.hpp:1899
_t< detail::transform_< list< Args... > >> transform
Return a new meta::list constructed by transforming all the elements in L with the unary invocable Fn...
Definition: meta.hpp:1855
apply< quote< concat >, ListOfLists > join
Joins a list of lists into a single list.
Definition: meta.hpp:1786
Tiny meta-programming library.
Definition: generate_n.hpp:41
Definition: for_each.hpp:111
Definition: repeat_n.hpp:43
Definition: single.hpp:41
Definition: transform.hpp:201
Lazily applies an unary function to each element in the source range that returns another range (poss...
Definition: for_each.hpp:47
Definition: for_each.hpp:58
Definition: for_each.hpp:73
Definition: for_each.hpp:86
Definition: for_each.hpp:99