Skip to content
Open
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
1 change: 1 addition & 0 deletions libraries/core/lib/morpheus/core/functional/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ target_sources(MorpheusCore
PUBLIC
FILE_SET HEADERS
FILES
curry.hpp
function_ref.hpp
overload.hpp
)
31 changes: 31 additions & 0 deletions libraries/core/lib/morpheus/core/functional/curry.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <concepts>
#include <functional>
#include <utility>

namespace morpheus::functional
{

#if (__cpp_explicit_this_parameter >= 202110L)

/// \fn curry
/// Currying is a technique in which we consider a function takign multuiple arguments and turn it into a function which takes a single argument and
/// returns a funtion to handle the remaining arguments.
/// \note
/// [Applicative: The Forgotten Functional Pattern in C++ - Ben Deane - CppNow 2023](https://youtu.be/At-b4PHNxMg?si=hDI3zgmfPwrrIxoe&t=1313)
template <typename F, typename... Args>
constexpr auto curry(F&& f, Args&&... args) -> decltype(auto)
{
if constexpr (std::invocable<F, Args...>)
return std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
else
{
return [f = std::forward<F>(f), ... args = std::forward<Args>(args)]<typename Self, typename... As>(this Self&& self, As&&... as) -> decltype(auto)
{ return curry(std::forward_like<Self>(f), std::forward_like<Self>(args)..., std::forward<As>(as)...); };
}
}

#endif // (__cpp_explicit_this_parameter >= 202110L)

} // namespace morpheus::functional
1 change: 1 addition & 0 deletions libraries/core/tests/functional/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target_sources(MorpheusCoreTests
PRIVATE
curry.tests.cpp
function_ref.tests.cpp
)
56 changes: 56 additions & 0 deletions libraries/core/tests/functional/curry.tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "morpheus/core/functional/curry.hpp"

#include <catch2/catch_all.hpp>

namespace morpheus::functional
{

#if (__cpp_explicit_this_parameter >= 202110L)

TEST_CASE("Verify partial application via currying", "[morpheus.functional.curry]")
{
SECTION("Zero parameters")
{
constexpr auto shouldCall = []() { SUCCEED(); };
curry(shouldCall);
}
SECTION("One parameters")
{
constexpr auto expectedParam1 = 1;
constexpr auto identity = [](auto a) { return a; };
curry(identity, expectedParam1);
REQUIRE(expectedParam1 == identity(expectedParam1));
}
SECTION("Two parameters")
{
constexpr auto expectedParam1 = 1;
constexpr auto expectedParam2 = 2;
constexpr auto add = [](auto a, auto b)
{
REQUIRE(a == expectedParam1);
REQUIRE(b == expectedParam2);
return a + b;
};
constexpr auto add1 = curry(add, expectedParam1);
REQUIRE(add1(expectedParam2) == (expectedParam1 + expectedParam2));
}
SECTION("Three parameters")
{
constexpr auto expectedParam1 = 1;
constexpr auto expectedParam2 = 2;
constexpr auto expectedParam3 = 3;
constexpr auto add = [](auto a, auto b, auto c)
{
REQUIRE(a == expectedParam1);
REQUIRE(b == expectedParam2);
REQUIRE(c == expectedParam3);
return a + b + c;
};
constexpr auto add1 = curry(add, expectedParam1);
REQUIRE(add1(expectedParam2, expectedParam3) == (expectedParam1 + expectedParam2 + expectedParam3));
}
}

#endif // (__cpp_explicit_this_parameter >= 202110L)

} // namespace morpheus::functional
Loading