LeechCraft 0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
applicative.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <optional>
12#include "either.h"
13
14namespace LC
15{
16namespace Util
17{
18 template<typename Applicative>
20
21 template<typename AF, typename AV>
22 using GSLResult_t = typename InstanceApplicative<AF>::template GSLResult<AV>::Type_t;
23
24 template<template<typename...> class Applicative, typename... Args, typename T>
25 auto Pure (const T& v)
26 {
28 }
29
30 template<typename Applicative, typename T>
31 auto Pure (const T& v) -> decltype (InstanceApplicative<Applicative>::Pure (v))
32 {
34 }
35
36 template<typename AF, typename AV>
37 GSLResult_t<AF, AV> GSL (const AF& af, const AV& av)
38 {
40 }
41
42 template<typename AF, typename AV>
43 auto operator* (const AF& af, const AV& av) -> decltype (GSL (af, av))
44 {
45 return GSL (af, av);
46 }
47
48 // Implementations
49
50 template<typename T>
52 {
53 using Type_t = std::optional<T>;
54
55 template<typename>
56 struct GSLResult;
57
58 template<typename V>
60 {
61 using Type_t = std::optional<std::result_of_t<T (const V&)>>;
62 };
63
64 template<typename U>
65 static std::optional<U> Pure (const U& v)
66 {
67 return { v };
68 }
69
70 template<typename AV>
71 static GSLResult_t<Type_t, AV> GSL (const Type_t& f, const AV& v)
72 {
73 if (!f || !v)
74 return {};
75
76 return { (*f) (*v) };
77 }
78 };
79
80 template<typename L, typename R>
82 {
84
85 template<typename>
86 struct GSLResult;
87
88 template<typename V>
89 struct GSLResult<Either<L, V>>
90 {
91 using Type_t = Either<L, std::result_of_t<R (const V&)>>;
92 };
93
94 template<typename RP>
95 static Either<L, RP> Pure (const RP& v)
96 {
97 return Either<L, RP>::Right (v);
98 }
99
100 template<typename AV>
101 static GSLResult_t<Type_t, AV> GSL (const Type_t& f, const AV& v)
102 {
103 using R_t = GSLResult_t<Type_t, AV>;
104
105 if (f.IsLeft ())
106 return R_t::Left (f.GetLeft ());
107
108 if (v.IsLeft ())
109 return R_t::Left (v.GetLeft ());
110
111 return R_t::Right (f.GetRight () (v.GetRight ()));
112 }
113 };
114
115}
116}
static Either Right(R &&r)
Definition either.h:124
Container< T > Filter(const Container< T > &c, F f)
Definition prelude.h:118
typename InstanceApplicative< AF >::template GSLResult< AV >::Type_t GSLResult_t
Definition applicative.h:22
auto Pure(const T &v)
Definition applicative.h:25
GSLResult_t< AF, AV > GSL(const AF &af, const AV &av)
Definition applicative.h:37
auto operator*(const AF &af, const AV &av) -> decltype(GSL(af, av))
Definition applicative.h:43
Definition constants.h:15
STL namespace.
static Either< L, RP > Pure(const RP &v)
Definition applicative.h:95
static GSLResult_t< Type_t, AV > GSL(const Type_t &f, const AV &v)
static std::optional< U > Pure(const U &v)
Definition applicative.h:65
static GSLResult_t< Type_t, AV > GSL(const Type_t &f, const AV &v)
Definition applicative.h:71