repeat.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 
31 #include <atria/prelude/comp.hpp>
32 
33 namespace atria {
34 namespace xform {
35 
36 namespace detail {
37 
38 struct repeat_rf_gen
39 {
40  template <typename ReducingFnT,
41  typename ValueT>
42  struct apply
43  {
44  ReducingFnT step;
45  ValueT value;
46 
47  template <typename StateT, typename ...InputTs>
48  auto operator() (StateT&& s, InputTs&& ...is)
50  step(std::forward<StateT>(s), std::forward<InputTs>(is)..., value))
51  };
52 };
53 
54 } // namespace detail
55 
56 template <typename T>
57 using repeat_t = transducer_impl<detail::repeat_rf_gen, T>;
58 
63 template <typename ValueT>
64 constexpr auto repeat(ValueT&& r)
66 {
68  std::forward<ValueT>(r) };
69 }
70 
71 template <typename ValueT, typename ...ValueTs>
72 constexpr auto repeat(ValueT&& r, ValueTs&& ...rs)
74  comp(repeat(std::forward<ValueT>(r)),
75  repeat(std::forward<ValueTs>(rs))...))
76 
77 
81 template <typename IntegralT, typename ...ValueTs>
82 constexpr auto repeatn(IntegralT&& n, ValueTs&& ...rs)
84  comp(repeat(std::forward<ValueTs>(rs)...),
85  take(std::forward<IntegralT>(n))))
86 
87 } // namespace xform
88 } // namespace atria
#define ABL_DECLTYPE_RETURN(body_expr)
Utility for defining generic functions with a deduced return type, that are composed of a single expr...
Definition: utils.hpp:109
constexpr auto repeat(ValueT &&r) -> repeat_t< estd::decay_t< ValueT > >
Generator transducer produces the values passed as parameter infinitely.
Definition: repeat.hpp:64
constexpr auto repeatn(IntegralT &&n, ValueTs &&...rs) -> decltype(comp(repeat(std::forward< ValueTs >(rs)...), take(std::forward< IntegralT >(n))))
Generator transducer produces the values passed as parameter up to n times.
Definition: repeat.hpp:82
auto take(IntegralT &&n) -> take_t< estd::decay_t< IntegralT > >
Similar to clojure.core/take$1.
Definition: take.hpp:87
Utility to write simple transducers easily.
auto comp(F &&f) -> F &&
Right-to left function composition.
Definition: comp.hpp:80
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
Fork me on GitHub