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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ add_compile_options(
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Workaround for std::expected not available in clang
add_compile_options(
-stdlib=libstdc++ -D__cpp_concepts=202002 -Wno-builtin-macro-redefined
-stdlib=libstdc++ -D__cpp_concepts=202002 -Wno-builtin-macro-redefined -Wno-missing-braces
)
endif()

Expand Down
4 changes: 3 additions & 1 deletion include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Pls keep the filenames sorted
set(INCLUDE_SOURCE_FILES
functional/detail/closure.hpp
functional/detail/concepts.hpp
functional/detail/not_tuple.hpp
functional/detail/fwd_macro.hpp
functional/detail/traits.hpp
functional/and_then.hpp
functional/fail.hpp
Expand All @@ -19,6 +20,7 @@ set(INCLUDE_SOURCE_FILES
functional/recover.hpp
functional/transform_error.hpp
functional/transform.hpp
functional/utility.hpp
)

add_library(${PROJECT_NAME} INTERFACE ${INCLUDE_SOURCE_FILES})
Expand Down
18 changes: 7 additions & 11 deletions include/functional/and_then.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "functional/detail/concepts.hpp"
#include "functional/functor.hpp"
#include "functional/fwd.hpp"
#include "functional/utility.hpp"

#include <concepts>
#include <utility>
Expand All @@ -18,7 +19,7 @@ namespace fn {
constexpr inline struct and_then_t final {
auto operator()(auto &&fn) const noexcept -> functor<and_then_t, decltype(fn)>
{
return {std::forward<decltype(fn)>(fn)};
return {FWD(fn)};
}

struct apply;
Expand All @@ -27,30 +28,25 @@ constexpr inline struct and_then_t final {
struct and_then_t::apply final {
static auto operator()(some_expected auto &&v, auto &&fn) noexcept
-> decltype(auto)
requires std::invocable<decltype(fn),
decltype(std::forward<decltype(v)>(v).value())>
requires std::invocable<decltype(fn), decltype(FWD(v).value())>
&& (!std::is_void_v<decltype(v.value())>)

@atomgalaxy atomgalaxy Jan 17, 2024 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a feeling you might be better off if you made a "some_void_expected" concept...

In that case, you probably don't need this restriction at all, since the above invocable will SFINAE anyway, and then the below one is the right one if you also constrain it with some_void_expected

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, will switch to concepts in a separate PR

{
return std::forward<decltype(v)>(v).and_then(
std::forward<decltype(fn)>(fn));
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 std::forward<decltype(v)>(v).and_then(
std::forward<decltype(fn)>(fn));
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(std::forward<decltype(v)>(v).value())>
requires std::invocable<decltype(fn), decltype(FWD(v).value())>
{
return std::forward<decltype(v)>(v).and_then(
std::forward<decltype(fn)>(fn));
return FWD(v).and_then(FWD(fn));
}
};

Expand Down
38 changes: 38 additions & 0 deletions include/functional/detail/closure.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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_DETAIL_CLOSURE
#define INCLUDE_FUNCTIONAL_DETAIL_CLOSURE

#include "fwd_macro.hpp"
#include "traits.hpp"

#include <type_traits>
#include <utility>

namespace fn::detail {

template <std::size_t I, typename T> struct _element {
static_assert(not std::is_rvalue_reference_v<T>);
T v;
};

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
-> decltype(FWD(fn)(FWD(args)..., apply_const_t<Self, Ts &&>(
self._element<Is, Ts>::v)...))
{
return FWD(fn)(FWD(args)...,
apply_const_t<Self, Ts &&>(self._element<Is, Ts>::v)...);
}
};

} // namespace fn::detail

#endif // INCLUDE_FUNCTIONAL_DETAIL_CLOSURE
13 changes: 13 additions & 0 deletions include/functional/detail/fwd_macro.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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_DETAIL_FWD_MACRO
#define INCLUDE_FUNCTIONAL_DETAIL_FWD_MACRO

// This FWD macro is a functional equivalent to std::forward<decltype(v)>(v),
// but it saves compilation time (and typing) when used frequently.
#define FWD(...) static_cast<decltype(__VA_ARGS__) &&>(__VA_ARGS__)

#endif // INCLUDE_FUNCTIONAL_DETAIL_FWD_MACRO
54 changes: 0 additions & 54 deletions include/functional/detail/not_tuple.hpp

This file was deleted.

12 changes: 1 addition & 11 deletions include/functional/detail/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ template <typename T>
requires(!std::is_empty_v<T>)
extern T const &_as_value<T const &>;

template <typename T> using as_value_t = decltype(_as_value<T>);

// Add const to second type, if first type is const
template <typename T, typename V> extern V _apply_const;
template <typename T, typename V> extern V const _apply_const<T const &, V>;
Expand All @@ -36,15 +34,7 @@ template <typename T, typename V>
extern V const &&_apply_const<T const &, V &&>;

template <typename T, typename V>
using apply_const_t = decltype(_apply_const<T &, V>);

// NOTE: Unlike apply_const_t above this is not exact: prvalue parameters are
// changed to xvalue. This is meant to disable copying of the return value.
template <typename T>
constexpr auto apply_const(auto &&v) noexcept -> decltype(auto)
{
return static_cast<apply_const_t<T, decltype(v)>>(v);
}
using apply_const_t = decltype(detail::_apply_const<T &, V>);

} // namespace fn::detail

Expand Down
34 changes: 14 additions & 20 deletions include/functional/fail.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "functional/detail/concepts.hpp"
#include "functional/functor.hpp"
#include "functional/fwd.hpp"
#include "functional/utility.hpp"

#include <concepts>
#include <type_traits>
Expand All @@ -19,7 +20,7 @@ namespace fn {
constexpr inline struct fail_t final {
auto operator()(auto &&fn) const noexcept -> functor<fail_t, decltype(fn)>
{
return {std::forward<decltype(fn)>(fn)};
return {FWD(fn)};
}

struct apply;
Expand All @@ -28,20 +29,16 @@ constexpr inline struct fail_t final {
struct fail_t::apply final {
static auto operator()(some_expected auto &&v, auto &&fn) noexcept
-> decltype(auto)
requires std::invocable<decltype(fn),
decltype(std::forward<decltype(v)>(v).value())>
requires std::invocable<decltype(fn), decltype(FWD(v).value())>
&& (!std::is_void_v<decltype(v.value())>)
{
using type = std::remove_cvref_t<decltype(v)>;
static_assert(
std::is_convertible_v<
std::invoke_result_t<
decltype(fn), decltype(std::forward<decltype(v)>(v).value())>,
typename type::error_type>);
return std::forward<decltype(v)>(v).and_then( //
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>{
std::forward<decltype(fn)>(fn)(std::forward<decltype(arg)>(arg))};
return std::unexpected<typename type::error_type>{FWD(fn)(FWD(arg))};
});
}

Expand All @@ -53,25 +50,22 @@ struct fail_t::apply final {
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 std::forward<decltype(v)>(v).and_then( //
return FWD(v).and_then( //
[&fn]() noexcept -> type {
return std::unexpected<typename type::error_type>{
std::forward<decltype(fn)>(fn)()};
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(std::forward<decltype(v)>(v).value())>
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(std::forward<decltype(v)>(v).value())>>);
std::is_void_v<
std::invoke_result_t<decltype(fn), decltype(FWD(v).value())>>);
if (v.has_value()) {
std::forward<decltype(fn)>(fn)(
std::forward<decltype(v)>(v).value()); // side-effects only
FWD(fn)(FWD(v).value()); // side-effects only
}
return type{std::nullopt};
}
Expand Down
31 changes: 9 additions & 22 deletions include/functional/functor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,39 @@
#ifndef INCLUDE_FUNCTIONAL_FUNCTOR
#define INCLUDE_FUNCTIONAL_FUNCTOR

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

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

namespace fn {

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

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 Functor, typename V, typename... Args>
concept monadic_invocable
= some_monadic_type<V> //
&& std::invocable<typename Functor::apply, V, Args...>;

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);
detail::not_tuple<detail::as_value_t<Args>...> data;
using data_t = closure<as_value_t<Args>...>;
data_t data;

static_assert(sizeof...(Args) > 0); // NOTE Consider relaxing
static_assert(std::is_empty_v<Functor>
&& std::is_empty_v<typename Functor::apply>
&& std::is_default_constructible_v<Functor>
&& std::is_default_constructible_v<typename Functor::apply>);
static_assert(std::is_empty_v<functor_type> && std::is_empty_v<functor_apply>
&& 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)
requires std::same_as<std::remove_cvref_t<decltype(self)>, functor>
&& monadic_invocable<functor_type, decltype(v), Args...>
{
return
[&v, &self]<unsigned... I>(
std::integer_sequence<unsigned, I...>) noexcept -> decltype(auto) {
return Functor::apply::template operator()( //
std::forward<decltype(v)>(v), //
detail::get<I>(std::forward<decltype(self)>(self).data)...);
}(std::make_integer_sequence<unsigned, size>{});
return data_t::invoke(FWD(self).data, functor_apply{}, FWD(v));
}
};

Expand Down
Loading