13 #ifndef __STOUT_LAMBDA_HPP__ 14 #define __STOUT_LAMBDA_HPP__ 19 #include <type_traits> 23 #include <glog/logging.h> 41 template <
typename...>
class Iterable,
46 Iterable<V>
map(F&&
f,
const Iterable<U, Us...>& input)
52 std::inserter(output, output.begin()),
59 template <
typename...>
class OutputIterable,
60 template <
typename...>
class InputIterable,
65 OutputIterable<V>
map(F&&
f,
const InputIterable<U, Us...>& input)
67 OutputIterable<V> output;
71 std::inserter(output, output.begin()),
78 template <
typename...>
class Iterable,
82 typename =
typename std::enable_if<
83 !std::is_same<U, V>::value>
::type,
85 Iterable<V>
map(F&&
f, Iterable<U, Us...>&& input)
89 std::make_move_iterator(input.begin()),
90 std::make_move_iterator(input.end()),
91 std::inserter(output, output.begin()),
98 template <
typename...>
class Iterable,
101 typename =
typename std::enable_if<
104 Iterable<U, Us...>&&
map(F&&
f, Iterable<U, Us...>&& iterable)
107 std::make_move_iterator(iterable.begin()),
108 std::make_move_iterator(iterable.end()),
111 return std::move(iterable);
116 template <
typename...>
class OutputIterable,
117 template <
typename...>
class InputIterable,
122 OutputIterable<V>
map(F&&
f, InputIterable<U, Us...>&& input)
124 OutputIterable<V> output;
126 std::make_move_iterator(input.begin()),
127 std::make_move_iterator(input.end()),
128 std::inserter(output, output.begin()),
135 template <
typename...>
class OutputIterable,
139 OutputIterable<V>
map(F&&
f, std::initializer_list<U> input)
141 OutputIterable<V> output;
145 std::inserter(output, output.begin()),
155 std::vector<V>
map(F&&
f, std::initializer_list<U> input)
157 std::vector<V> output;
161 std::inserter(output, output.begin()),
172 template <
typename...>
class OutputIterable,
173 template <
typename...>
class InputIterable1,
174 template <
typename...>
class InputIterable2,
179 OutputIterable<std::pair<U1, U2>>
zipto(
180 const InputIterable1<U1, U1s...>& input1,
181 const InputIterable2<U2, U2s...>& input2)
183 OutputIterable<std::pair<U1, U2>> output;
185 auto iterator1 = input1.begin();
186 auto iterator2 = input2.begin();
188 auto inserter = std::inserter(output, output.begin());
191 while (iterator1 != input1.end() && iterator2 != input2.end()) {
192 inserter = std::make_pair(*iterator1, *iterator2);
207 template <
typename...>
class InputIterable1,
208 template <
typename...>
class InputIterable2,
214 const InputIterable1<U1, U1s...>& input1,
215 const InputIterable2<U2, U2s...>& input2)
225 auto iterator1 = input1.begin();
226 auto iterator2 = input2.begin();
229 while (iterator1 != input1.end() && iterator2 != input2.end()) {
230 output.
put(*iterator1, *iterator2);
239 #define RETURN(...) -> decltype(__VA_ARGS__) { return __VA_ARGS__; } 253 template <
typename T,
typename Args>
254 auto operator()(T&&, Args&& args)
const 255 RETURN(std::get<I - 1>(std::forward<Args>(args)))
263 template <
typename T,
typename Args>
264 auto operator()(T&& t, Args&&)
const 265 RETURN(std::forward<T>(t))
269 template <
typename F,
typename... BoundArgs>
273 std::tuple<BoundArgs...> bound_args;
275 template <
typename T,
typename Args>
276 static auto expand(T&& t, Args&& args)
278 std::forward<T>(t), std::forward<Args>(args)))
286 template <
typename F_,
typename BoundArgs_,
typename Args, std::size_t... Is>
287 static auto invoke_expand(
289 BoundArgs_&& bound_args,
295 std::get<Is>(std::forward<BoundArgs_>(bound_args)),
296 std::forward<Args>(args))...))
299 template <
typename... BoundArgs_>
300 explicit Partial(
const F& f, BoundArgs_&&... args)
301 :
f(f), bound_args(std::forward<BoundArgs_>(args)...) {}
303 template <
typename... BoundArgs_>
304 explicit Partial(F&& f, BoundArgs_&&... args)
305 :
f(std::move(f)), bound_args(std::forward<BoundArgs_>(args)...) {}
307 Partial(
const Partial&) =
default;
308 Partial(Partial&&) =
default;
310 Partial& operator=(
const Partial&) =
default;
311 Partial& operator=(Partial&&) =
default;
313 template <
typename... Args>
314 auto operator()(Args&&... args) &
319 std::forward_as_tuple(std::forward<Args>(args)...)))
321 template <
typename... Args>
322 auto operator()(Args&&... args)
const &
327 std::forward_as_tuple(std::forward<Args>(args)...)))
329 template <
typename... Args>
330 auto operator()(Args&&... args) &&
333 std::move(bound_args),
335 std::forward_as_tuple(std::forward<Args>(args)...)))
337 template <
typename... Args>
338 auto operator()(Args&&... args)
const &&
341 std::move(bound_args),
343 std::forward_as_tuple(std::forward<Args>(args)...)))
360 template <
typename F,
typename... Args>
367 typename std::decay<F>::type,
369 return R(std::forward<F>(
f), std::forward<Args>(args)...);
381 template <
typename R>
384 template <
typename F,
typename... Args>
387 return cpp17::invoke(std::forward<F>(
f), std::forward<Args>(args)...);
395 template <
typename F,
typename... Args>
398 cpp17::invoke(std::forward<F>(
f), std::forward<Args>(args)...);
413 template <
typename F>
417 template <
typename R,
typename... Args>
423 typename std::enable_if<
424 !std::is_same<F, CallableOnce>::value &&
425 (std::is_same<R, void>::value ||
428 cpp17::invoke(std::declval<F>(), std::declval<Args>()...)),
432 :
f(
new CallableFn<
typename std::decay<F>::type>(std::forward<F>(
f))) {}
443 return std::move(*
f)(std::forward<Args>(args)...);
449 virtual ~Callable() =
default;
450 virtual R operator()(Args&&...) && = 0;
453 template <
typename F>
454 struct CallableFn : Callable
458 CallableFn(
const F& f) :
f(f) {}
459 CallableFn(F&& f) :
f(std::move(f)) {}
461 R operator()(Args&&... args) &&
override 467 std::unique_ptr<Callable>
f;
475 template <
typename F,
typename... Args>
476 struct is_bind_expression<
lambda::internal::Partial<F, Args...>>
481 #endif // __STOUT_LAMBDA_HPP__
F && f
Definition: defer.hpp:270
CallableOnce(F &&f)
Definition: lambda.hpp:431
hashmap< U1, U2 > zip(const InputIterable1< U1, U1s... > &input1, const InputIterable2< U2, U2s... > &input2)
Definition: lambda.hpp:213
internal::Partial< typename std::decay< F >::type, typename std::decay< Args >::type... > partial(F &&f, Args &&...args)
Definition: lambda.hpp:364
Definition: type_utils.hpp:619
Definition: lambda.hpp:30
void operator()(F &&f, Args &&...args)
Definition: lambda.hpp:396
R operator()(F &&f, Args &&...args)
Definition: lambda.hpp:385
Definition: hashmap.hpp:38
Definition: lambda.hpp:270
Definition: lambda.hpp:250
make_integer_sequence< std::size_t, N > make_index_sequence
Definition: cpp14.hpp:61
process::Future< Nothing > transform(process::Owned< Reader< T >> &&reader, const std::function< std::string(const T &)> &func, process::http::Pipe::Writer writer)
This is a helper function that reads records from a Reader, applies a transformation to the records a...
Definition: recordio.hpp:112
void put(const Key &key, Value &&value)
Definition: hashmap.hpp:104
Iterable< V > map(F &&f, const Iterable< U, Us... > &input)
Definition: lambda.hpp:46
Definition: attributes.hpp:24
R operator()(Args...args)&&
Definition: lambda.hpp:440
Try< uint32_t > type(const std::string &path)
OutputIterable< std::pair< U1, U2 > > zipto(const InputIterable1< U1, U1s... > &input1, const InputIterable2< U2, U2s... > &input2)
Definition: lambda.hpp:179
Definition: lambda.hpp:382
Try< Nothing > bind(int_fd s, const Address &address)
Definition: network.hpp:46
#define RETURN(...)
Definition: lambda.hpp:239
Definition: lambda.hpp:414