-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhof.hpp
39 lines (32 loc) · 988 Bytes
/
hof.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// Copyright (c) 2016-present DeepGrace (complex dot invoke at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/deepgrace/monster
//
#ifndef HOF_HPP
#define HOF_HPP
#include <algorithm>
#include <numeric>
namespace monster
{
template <typename F, typename R>
R mapf(F&& f, R&& r)
{
std::transform(std::begin(r), std::end(r), std::begin(r), std::forward<F>(f));
return r;
}
template <typename F, typename R, typename T>
constexpr T foldl(F&& f, R&& r, T t)
{
return std::accumulate(std::begin(r), std::end(r), std::move(t), std::forward<F>(f));
}
template <typename F, typename R, typename T>
constexpr T foldr(F&& f, R&& r, T t)
{
return std::accumulate(std::rbegin(r), std::rend(r), std::move(t), std::forward<F>(f));
}
}
#endif