Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
PPIndentWidth: -1
QualifierAlignment: Leave
QualifierAlignment: Custom
QualifierOrder: ['static', 'constexpr', 'inline', 'friend', 'type', 'const', 'volatile']
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
Expand Down
1 change: 1 addition & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(INCLUDE_SOURCE_FILES
functional/detail/fwd_macro.hpp
functional/detail/traits.hpp
functional/and_then.hpp
functional/concepts.hpp
functional/fail.hpp
functional/functor.hpp
functional/fwd.hpp
Expand Down
43 changes: 22 additions & 21 deletions include/functional/and_then.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef INCLUDE_FUNCTIONAL_AND_THEN
#define INCLUDE_FUNCTIONAL_AND_THEN

#include "functional/detail/concepts.hpp"
#include "functional/concepts.hpp"
#include "functional/functor.hpp"
#include "functional/fwd.hpp"
#include "functional/utility.hpp"
Expand All @@ -15,9 +15,25 @@
#include <utility>

namespace fn {
template <typename Fn, typename V>
concept invocable_and_then
= (some_expected_non_void<V> && requires(Fn &&fn, V &&v) {
{
std::invoke(FWD(fn), FWD(v).value())
} -> same_kind<V>;
}) || (some_expected_void<V> && requires(Fn &&fn) {
{
std::invoke(FWD(fn))
} -> same_kind<V>;
}) || (some_optional<V> && requires(Fn &&fn, V &&v) {
{
std::invoke(FWD(fn), FWD(v).value())
} -> same_kind<V>;
});

constexpr inline struct and_then_t final {
auto operator()(auto &&fn) const noexcept -> functor<and_then_t, decltype(fn)>
constexpr auto operator()(auto &&fn) const noexcept
-> functor<and_then_t, decltype(fn)>
{
return {FWD(fn)};
}
Expand All @@ -26,25 +42,10 @@ constexpr inline struct and_then_t final {
} and_then = {};

struct and_then_t::apply final {
static auto operator()(some_expected auto &&v, auto &&fn) noexcept
-> decltype(auto)
requires std::invocable<decltype(fn), decltype(FWD(v).value())>
&& (!std::is_void_v<decltype(v.value())>)
{
return FWD(v).and_then(FWD(fn));
}

static auto operator()(some_expected auto &&v, auto &&fn) noexcept
-> decltype(auto)
requires std::invocable<decltype(fn)>
&& (std::is_void_v<decltype(v.value())>)
{
return FWD(v).and_then(FWD(fn));
}

static auto operator()(some_optional auto &&v, auto &&fn) noexcept
-> decltype(auto)
requires std::invocable<decltype(fn), decltype(FWD(v).value())>
static constexpr auto operator()(some_monadic_type auto &&v,
auto &&fn) noexcept
-> same_kind<decltype(v)> auto
requires invocable_and_then<decltype(fn), decltype(v)>
{
return FWD(v).and_then(FWD(fn));
}
Expand Down
83 changes: 83 additions & 0 deletions include/functional/concepts.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2024 Bronek Kozicki
//
// Distributed under the ISC License. See accompanying file LICENSE.md
// or copy at https://opensource.org/licenses/ISC

#ifndef INCLUDE_FUNCTIONAL_CONCEPTS
#define INCLUDE_FUNCTIONAL_CONCEPTS

#include "functional/detail/concepts.hpp"

#include <concepts>
#include <expected>
#include <functional>
#include <type_traits>

namespace fn {
template <typename T>
concept some_expected = detail::_is_some_expected<T &>;

template <typename T>
concept some_expected_void
= some_expected<T>
&& std::same_as<void, typename std::remove_cvref_t<T>::value_type>;

template <typename T>
concept some_expected_non_void
= some_expected<T>
&& !std::same_as<void, typename std::remove_cvref_t<T>::value_type>;

template <typename T>
concept some_optional = detail::_is_some_optional<T &>;

template <typename T>
concept some_monadic_type = some_expected<T> || some_optional<T>;

template <typename T, typename U>
concept similar_expected
= some_expected<T> && some_expected<U>
&& std::same_as<typename std::remove_cvref_t<T>::error_type,
typename std::remove_cvref_t<U>::error_type>;

// NOTE `same_kind` is a fundamental concept in category theory; it allows
// transformation of a value_type, but not an error_type (where applicable)
template <typename T, typename U>
concept same_kind
= (some_expected<T> && some_expected<U>
&& std::same_as<typename std::remove_cvref_t<T>::error_type,
typename std::remove_cvref_t<U>::error_type>)
|| (some_optional<T> && some_optional<U>);

// NOTE similar to the above, but not nearly as useful since it only allows
// transformation of an error type
template <typename T, typename U>
concept same_value_kind
= (some_expected<T> && some_expected<U>
&& std::same_as<typename std::remove_cvref_t<T>::value_type,
typename std::remove_cvref_t<U>::value_type>)
|| (some_optional<T> && some_optional<U>
&& std::same_as<typename std::remove_cvref_t<U>::value_type,
typename std::remove_cvref_t<T>::value_type>);

template <typename T, typename U>
concept same_monadic_type_as = same_kind<T, U> && same_value_kind<T, U>;

template <class T>
concept convertible_to_unexpected = requires {
static_cast<std::unexpected<std::remove_cvref_t<T>>>(std::declval<T>());
};

template <class T, typename E>
concept convertible_to_expected
= (not std::same_as<T, void> && requires {
static_cast<std::expected<std::remove_cvref_t<T>, E>>(
std::declval<T>());
}) || (std::same_as<T, void>);

template <class T>
concept convertible_to_optional = requires {
static_cast<std::optional<std::remove_cvref_t<T>>>(std::declval<T>());
};
} // namespace fn

#endif // INCLUDE_FUNCTIONAL_CONCEPTS
2 changes: 1 addition & 1 deletion include/functional/detail/closure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ template <typename, typename... Ts> struct closure_base;
template <std::size_t... Is, typename... Ts>
struct closure_base<std::index_sequence<Is...>, Ts...> : _element<Is, Ts>... {
template <typename Self, typename Fn, typename... Args>
static auto invoke(Self &&self, Fn &&fn, Args &&...args) noexcept
static constexpr auto invoke(Self &&self, Fn &&fn, Args &&...args) noexcept
-> decltype(FWD(fn)(FWD(args)..., apply_const_t<Self, Ts &&>(
self._element<Is, Ts>::v)...))
{
Expand Down
60 changes: 28 additions & 32 deletions include/functional/fail.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef INCLUDE_FUNCTIONAL_FAIL
#define INCLUDE_FUNCTIONAL_FAIL

#include "functional/detail/concepts.hpp"
#include "functional/concepts.hpp"
#include "functional/functor.hpp"
#include "functional/fwd.hpp"
#include "functional/utility.hpp"
Expand All @@ -16,9 +16,25 @@
#include <utility>

namespace fn {
template <typename Fn, typename V>
concept invocable_fail
= (some_expected_non_void<V> && requires(Fn &&fn, V &&v) {
{
std::invoke(FWD(fn), FWD(v).value())
} -> std::convertible_to<typename std::remove_cvref_t<V>::error_type>;
}) || (some_expected_void<V> && requires(Fn &&fn) {
{
std::invoke(FWD(fn))
} -> std::convertible_to<typename std::remove_cvref_t<V>::error_type>;
}) || (some_optional<V> && requires(Fn &&fn, V &&v) {
{
std::invoke(FWD(fn), FWD(v).value())
} -> std::same_as<void>;
});

constexpr inline struct fail_t final {
auto operator()(auto &&fn) const noexcept -> functor<fail_t, decltype(fn)>
constexpr auto operator()(auto &&fn) const noexcept
-> functor<fail_t, decltype(fn)>
{
return {FWD(fn)};
}
Expand All @@ -27,45 +43,25 @@ constexpr inline struct fail_t final {
} fail = {};

struct fail_t::apply final {
static auto operator()(some_expected auto &&v, auto &&fn) noexcept
-> decltype(auto)
requires std::invocable<decltype(fn), decltype(FWD(v).value())>
&& (!std::is_void_v<decltype(v.value())>)
static constexpr auto operator()(some_expected auto &&v, auto &&fn) noexcept
-> same_monadic_type_as<decltype(v)> auto
requires invocable_fail<decltype(fn), decltype(v)>
{
using type = std::remove_cvref_t<decltype(v)>;
static_assert(std::is_convertible_v<
std::invoke_result_t<decltype(fn), decltype(FWD(v).value())>,
typename type::error_type>);
return FWD(v).and_then( //
[&fn](auto &&arg) noexcept -> type {
return std::unexpected<typename type::error_type>{FWD(fn)(FWD(arg))};
[&fn](auto &&...arg) noexcept -> type {
return std::unexpected<typename type::error_type>{
std::invoke(FWD(fn), FWD(arg)...)};
});
}

static auto operator()(some_expected auto &&v, auto &&fn) noexcept
-> decltype(auto)
requires std::invocable<decltype(fn)>
&& (std::is_void_v<decltype(v.value())>)
static constexpr auto operator()(some_optional auto &&v, auto &&fn) noexcept
-> same_monadic_type_as<decltype(v)> auto
requires invocable_fail<decltype(fn), decltype(v)>
{
using type = std::remove_cvref_t<decltype(v)>;
static_assert(std::is_convertible_v<std::invoke_result_t<decltype(fn)>,
typename type::error_type>);
return FWD(v).and_then( //
[&fn]() noexcept -> type {
return std::unexpected<typename type::error_type>{FWD(fn)()};
});
}

static auto operator()(some_optional auto &&v, auto &&fn) noexcept
-> decltype(auto)
requires std::invocable<decltype(fn), decltype(FWD(v).value())>
{
using type = std::remove_cvref_t<decltype(v)>;
static_assert(
std::is_void_v<
std::invoke_result_t<decltype(fn), decltype(FWD(v).value())>>);
if (v.has_value()) {
FWD(fn)(FWD(v).value()); // side-effects only
std::invoke(FWD(fn), FWD(v).value());
}
return type{std::nullopt};
}
Expand Down
9 changes: 4 additions & 5 deletions include/functional/functor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
#ifndef INCLUDE_FUNCTIONAL_FUNCTOR
#define INCLUDE_FUNCTIONAL_FUNCTOR

#include "functional/concepts.hpp"
#include "functional/detail/closure.hpp"
#include "functional/detail/concepts.hpp"
#include "functional/utility.hpp"

#include <concepts>
#include <type_traits>
#include <utility>

namespace fn {

template <typename Functor, typename V, typename... Args>
concept monadic_invocable
= some_monadic_type<V> //
Expand All @@ -24,7 +23,7 @@ concept monadic_invocable
template <typename Functor, typename... Args> struct functor final {
using functor_type = Functor;
using functor_apply = typename functor_type::apply;
constexpr static unsigned size = sizeof...(Args);
static constexpr unsigned size = sizeof...(Args);
using data_t = closure<as_value_t<Args>...>;
data_t data;

Expand All @@ -33,8 +32,8 @@ template <typename Functor, typename... Args> struct functor final {
&& std::is_default_constructible_v<functor_type>
&& std::is_default_constructible_v<functor_apply>);

friend auto operator|(some_monadic_type auto &&v, auto &&self) noexcept
-> decltype(auto)
constexpr friend auto operator|(some_monadic_type auto &&v,
auto &&self) noexcept -> decltype(auto)
requires std::same_as<std::remove_cvref_t<decltype(self)>, functor>
&& monadic_invocable<functor_type, decltype(v), Args...>
{
Expand Down
52 changes: 26 additions & 26 deletions include/functional/inspect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,35 @@
#ifndef INCLUDE_FUNCTIONAL_INSPECT
#define INCLUDE_FUNCTIONAL_INSPECT

#include "functional/concepts.hpp"
#include "functional/detail/concepts.hpp"
#include "functional/functor.hpp"
#include "functional/fwd.hpp"
#include "functional/utility.hpp"

#include <concepts>
#include <type_traits>
#include <utility>

namespace fn {
template <typename Fn, typename V>
concept invocable_inspect
= (some_expected_non_void<V> && requires(Fn &&fn, V &&v) {
{
std::invoke(FWD(fn), std::as_const(v).value())
} -> std::same_as<void>;
}) || (some_expected_void<V> && requires(Fn &&fn) {
{
std::invoke(FWD(fn))
} -> std::same_as<void>;
}) || (some_optional<V> && requires(Fn &&fn, V &&v) {
{
std::invoke(FWD(fn), std::as_const(v).value())
} -> std::same_as<void>;
});

constexpr inline struct inspect_t final {
auto operator()(auto &&...fn) const noexcept
constexpr auto operator()(auto &&...fn) const noexcept
-> functor<inspect_t, decltype(fn)...>
requires(sizeof...(fn) > 0) && (sizeof...(fn) <= 2)
{
Expand All @@ -28,39 +45,22 @@ constexpr inline struct inspect_t final {
} inspect = {};

struct inspect_t::apply final {
static auto operator()(some_expected auto &&v, auto &&fn) noexcept
static constexpr auto operator()(some_expected auto &&v, auto &&fn) noexcept
-> decltype(v)
requires std::invocable<decltype(fn), decltype(std::as_const(v.value()))>
&& (!std::is_void_v<decltype(v.value())>)
requires invocable_inspect<decltype(fn), decltype(v)>
{
static_assert(std::is_void_v<std::invoke_result_t<
decltype(fn), decltype(std::as_const(v.value()))>>);
if (v.has_value()) {
FWD(fn)(std::as_const(v.value()));
}
return FWD(v);
}

static auto operator()(some_expected auto &&v, auto &&fn) noexcept
-> decltype(v)
requires std::invocable<decltype(fn)>
&& (std::is_void_v<decltype(v.value())>)
{
static_assert(std::is_void_v<std::invoke_result_t<decltype(fn)>>);
if (v.has_value()) {
FWD(fn)();
}
std::as_const(v).transform([&fn](auto const &...args) -> void {
std::invoke(FWD(fn), FWD(args)...); // side-effects only
});
return FWD(v);
}

static auto operator()(some_optional auto &&v, auto &&fn) noexcept
static constexpr auto operator()(some_optional auto &&v, auto &&fn) noexcept
-> decltype(v)
requires std::invocable<decltype(fn), decltype(std::as_const(v.value()))>
requires invocable_inspect<decltype(fn), decltype(v)>
{
static_assert(std::is_void_v<std::invoke_result_t<
decltype(fn), decltype(std::as_const(v.value()))>>);
if (v.has_value()) {
FWD(fn)(std::as_const(v.value()));
std::invoke(FWD(fn), std::as_const(v).value()); // side-effects only
}
return FWD(v);
}
Expand Down
Loading