take_nth.hpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2014, 2015 Ableton AG, Berlin. All rights reserved.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21 //
22 
27 #pragma once
28 
34 
35 namespace atria {
36 namespace xform {
37 
38 #if ABL_CXX14
39 
40 auto take_nth = [](auto nth) mutable
41 {
42  return [=](auto step) mutable
43  {
44  return [=](auto&& s, auto&& ...is) mutable
45  {
46  return with_state(
47  ABL_FORWARD(s),
48  [&](auto&& st)
49  {
50  return wrap_state(
51  step(ABL_FORWARD(st), ABL_FORWARD(is)...),
52  1);
53  },
54  [&](auto&& st)
55  {
56  auto count = state_wrapper_data(ABL_FORWARD(st));
57  return count != nth
58  ? wrap_state(
59  state_unwrap(ABL_FORWARD(st)),
60  count + 1)
61  : wrap_state(
62  step(state_unwrap(ABL_FORWARD(st)), ABL_FORWARD(is)...),
63  1);
64  });
65  };
66  };
67 };
68 
69 #else // ABL_CXX14
70 
71 namespace detail {
72 
73 struct take_nth_rf_gen
74 {
75  template <typename ReducingFnT,
76  typename IntegralT>
77  struct apply
78  {
79  ReducingFnT step;
80  IntegralT nth;
81 
82  template <typename StateT, typename ...InputTs>
83  auto operator() (StateT&& s, InputTs&& ...is)
84  -> decltype(wrap_state(step(state_unwrap(s), is...), nth))
85  {
86  using result_t = decltype(wrap_state(step(state_unwrap(s), is...), nth));
87  using complete_t = decltype(state_complete(s));
88 
89  using wrapped_t = meta::copy_decay_t<StateT, estd::decay_t<result_t>>;
90  using unwrapped_t = meta::copy_decay_t<StateT, estd::decay_t<complete_t>>;
91 
92  return with_state(
93  std::forward<StateT>(s),
94  [&](unwrapped_t&& st)
95  {
96  return wrap_state(
97  step(std::forward<unwrapped_t>(st),
98  std::forward<InputTs>(is)...),
99  1);
100  },
101  [&](wrapped_t&& st)
102  {
103  auto count = state_wrapper_data(std::forward<wrapped_t>(st));
104  return count != nth
105  ? wrap_state(
106  state_unwrap(std::forward<wrapped_t>(st)),
107  count + 1)
108  : wrap_state(
109  step(state_unwrap(std::forward<wrapped_t>(st)),
110  std::forward<InputTs>(is)...),
111  1);
112  });
113  }
114  };
115 };
116 
117 } // namespace detail
118 
119 template <typename T>
120 using take_nth_t = transducer_impl<detail::take_nth_rf_gen, T>;
121 
125 template <typename IntegralT>
126 auto take_nth(IntegralT&& n)
128 {
130  std::forward<IntegralT>(n) };
131 }
132 
133 #endif // ABL_CXX14
134 
135 } // namespace xform
136 } // namespace atria
constexpr auto count(InitT init=InitT{0}, StepT step=StepT{1}) -> count_t< InitT, StepT >
Generator transducer produces a sequence:
Definition: count.hpp:78
auto state_wrapper_data(TagT tag, T &&s, D &&) -> decltype(state_wrapper_data(tag, std::forward< T >(s)))
Utility function for easy overloading of state_traits::data for state wrappers with a specific tag...
auto state_unwrap(T &&s) -> decltype(state_traits_t< T >::unwrap(std::forward< T >(s)))
Convenience function for calling state_traits::unwrap
STL namespace.
auto take_nth(IntegralT &&n) -> take_nth_t< estd::decay_t< IntegralT > >
Similar to clojure.core/take_nth$1.
Definition: take_nth.hpp:126
auto with_state(StateT &&st, UnwrappedFn &&, WrappedFn &&fn) -> meta::lazy_enable_if_t< !std::is_same< estd::decay_t< StateT >, estd::decay_t< decltype(state_complete(st))> >::value, std::result_of< WrappedFn(StateT)> >
Given a value st that represents the state of a reduction, this function generically dispatches to th...
Definition: with_state.hpp:58
Utility to write simple transducers easily.
auto state_complete(T &&s) -> decltype(state_traits_t< T >::complete(std::forward< T >(s)))
Convenience function for calling state_traits::complete
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
auto wrap_state(StateT &&next, DataT &&data=DataT{}) -> state_wrapper< TagT, estd::decay_t< StateT >, estd::decay_t< DataT > >
Given a tag TagT and a state next and associated data, returns a state_wrapper instance.
Fork me on GitHub