Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional curry #287

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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/src/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
)
33 changes: 33 additions & 0 deletions libraries/core/src/morpheus/core/functional/curry.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#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
)
59 changes: 59 additions & 0 deletions libraries/core/tests/functional/curry.tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#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")
{
auto constexpr shouldCall = []()
{
SUCCEED();
};
curry(shouldCall);
}
SECTION("One parameters")
{
auto constexpr expectedParam1 = 1;
auto constexpr identity = [](auto a) { return a; };
curry(identity, expectedParam1);
REQUIRE(expectedParam1 == identity(expectedParam1));
}
SECTION("Two parameters")
{
auto constexpr expectedParam1 = 1;
auto constexpr expectedParam2 = 2;
auto constexpr add = [](auto a, auto b)
{
REQUIRE(a == expectedParam1);
REQUIRE(b == expectedParam2);
return a + b;
};
auto constexpr add1 = curry(add, expectedParam1);
REQUIRE(add1(expectedParam2) == (expectedParam1 + expectedParam2));
}
SECTION("Three parameters")
{
auto constexpr expectedParam1 = 1;
auto constexpr expectedParam2 = 2;
auto constexpr expectedParam3 = 3;
auto constexpr add = [](auto a, auto b, auto c)
{
REQUIRE(a == expectedParam1);
REQUIRE(b == expectedParam2);
REQUIRE(c == expectedParam3);
return a + b + c;
};
auto constexpr add1 = curry(add, expectedParam1);
REQUIRE(add1(expectedParam2, expectedParam3) == (expectedParam1 + expectedParam2 + expectedParam3));
}
}

#endif // (__cpp_explicit_this_parameter >= 202110L)

} // namespace morpheus::functional
Loading