-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some experiments with applicative for std::optional
- Loading branch information
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2023-present Vitaly Fanaskov | ||
// | ||
// fl -- Writer "monad" for C++ | ||
// Project home: https://github.com/vt4a2h/fl | ||
// | ||
// See LICENSE file for the further details. | ||
// | ||
#pragma once | ||
|
||
#include <optional> | ||
#include <type_traits> | ||
#include <concepts> | ||
|
||
namespace fl { | ||
|
||
namespace details { | ||
|
||
template<typename> | ||
constexpr bool is_optional = false; | ||
|
||
template<typename T> | ||
constexpr bool is_optional<std::optional<T>> = true; | ||
|
||
template<class T> | ||
concept IsOptional = is_optional<std::remove_cvref_t<T>>; | ||
|
||
template<class V> | ||
constexpr auto pure(V &&v) | ||
{ | ||
if constexpr (IsOptional<V>) { | ||
return std::forward<V>(v); | ||
} | ||
else { | ||
return std::make_optional(std::forward<V>(v)); | ||
} | ||
} | ||
|
||
} // namespace details | ||
|
||
template <class F, class ...Args> | ||
constexpr auto ap(F &&f, Args &&...args) noexcept(noexcept(std::invoke(std::forward<F>(f), std::forward<Args>(args)...))) | ||
requires (std::is_invocable_v<F, Args...>) | ||
{ | ||
return std::make_optional(std::invoke(std::forward<F>(f), std::forward<Args>(args)...)); | ||
} | ||
|
||
template <class F, details::IsOptional ...Args> | ||
constexpr auto ap(F &&f, Args &&...args) noexcept(noexcept(ap(std::forward<F>(f), *std::forward<Args>(args)...))) | ||
-> decltype(ap(std::forward<F>(f), *std::forward<Args>(args)...)) | ||
requires (!std::is_invocable_v<F, Args...>) | ||
{ | ||
if ((... && args)) { | ||
return ap(std::forward<F>(f), *std::forward<Args>(args)...); | ||
} else { | ||
return {}; | ||
} | ||
} | ||
|
||
template <class F, class ...Args> | ||
constexpr auto ap(F &&f, Args &&...args) noexcept(noexcept(ap(std::forward<F>(f), *details::pure(args)...))) | ||
requires (!std::is_invocable_v<F, Args...> && !(... && details::IsOptional<Args>)) | ||
{ | ||
return ap(std::forward<F>(f), *details::pure(args)...); | ||
} | ||
|
||
} // namespace fl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2023-present Vitaly Fanaskov | ||
// | ||
// fl -- Writer "monad" for C++ | ||
// Project home: https://github.com/vt4a2h/fl | ||
// | ||
// See LICENSE file for the further details. | ||
// | ||
#include "catch.hpp" | ||
|
||
#include <fl/utils/ap_optional.hpp> | ||
|
||
TEST_CASE("Applicative for optional") | ||
{ | ||
SECTION("Regular args and function") { | ||
const auto add = [](int a, int b) { return a + b; }; | ||
const int a = 1; | ||
const int b = 2; | ||
|
||
REQUIRE(fl::ap(add, a, b) == 3); | ||
} | ||
|
||
SECTION("Optional non-invocable") { | ||
const auto add = [](int a, const std::string &b) { return a + std::stoi(b); }; | ||
const auto a = std::make_optional<int>(1); | ||
const auto b = std::make_optional<std::string>("2"); | ||
|
||
REQUIRE(fl::ap(add, a, b) == 3); | ||
} | ||
|
||
SECTION("Optional invocable") { | ||
const auto add = [](std::optional<int> a, const std::optional<std::string> &b) { return *a + std::stoi(*b); }; | ||
const auto a = std::make_optional<int>(1); | ||
const auto b = std::make_optional<std::string>("2"); | ||
|
||
REQUIRE(fl::ap(add, a, b) == 3); | ||
} | ||
|
||
SECTION("Optional partial ap") { | ||
const auto add = [](int a, const std::string &b) { return a + std::stoi(b); }; | ||
const auto a = std::make_optional<int>(1); | ||
const auto b = std::string{"2"}; | ||
|
||
REQUIRE(fl::ap(add, a, b) == 3); | ||
} | ||
} |