functional.hpp 657 Bytes
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
#ifndef MIGRAPH_GUARD_RTGLIB_FUNCTIONAL_HPP
#define MIGRAPH_GUARD_RTGLIB_FUNCTIONAL_HPP

#include <utility>

namespace migraph {

namespace detail {

Paul's avatar
Paul committed
10
template <class R, class F>
Paul's avatar
Paul committed
11
12
13
14
struct fix_f
{
    F f;

Paul's avatar
Paul committed
15
    template <class... Ts>
Paul's avatar
Paul committed
16
17
18
19
20
21
22
23
24
    R operator()(Ts&&... xs) const
    {
        return f(*this, std::forward<Ts>(xs)...);
    }
};

} // namespace detail

/// Implements a fix-point combinator
Paul's avatar
Paul committed
25
template <class R, class F>
Paul's avatar
Paul committed
26
27
28
29
30
detail::fix_f<R, F> fix(F f)
{
    return {f};
}

Paul's avatar
Paul committed
31
template <class F>
Paul's avatar
Paul committed
32
33
34
35
36
auto fix(F f)
{
    return fix<void>(f);
}

Paul's avatar
Paul committed
37
template <class... Ts>
Paul's avatar
Paul committed
38
auto pack(Ts... xs)
39
{
Paul's avatar
Paul committed
40
    return [=](auto f) { return f(xs...); };
41
42
}

Paul's avatar
Paul committed
43
44
45
} // namespace migraph

#endif