rename kari namespace to kari_hpp

This commit is contained in:
BlackMATov
2020-12-09 23:20:11 +07:00
parent a9cb1035cc
commit 559899c42a
5 changed files with 84 additions and 87 deletions

View File

@@ -56,10 +56,10 @@ auto foo = [](int v1, int v2, int v3) {
return v1 + v2 + v3;
};
auto c0 = kari::curry(foo); // currying of `foo` function
auto c1 = c0(10); // apply to first argument
auto c2 = c1(20); // apply to second argument
auto rr = c2(12); // apply to third argument and call the `foo` function
auto c0 = curry(foo); // currying of `foo` function
auto c1 = c0(10); // apply to first argument
auto c2 = c1(20); // apply to second argument
auto rr = c2(12); // apply to third argument and call the `foo` function
// output: 42
std::cout << rr << std::endl;
@@ -72,9 +72,9 @@ auto foo = [](int v1, int v2, int v3, int v4) {
return v1 + v2 + v3 + v4;
};
auto c0 = kari::curry(foo); // currying
auto c1 = c0(15, 20); // partial application of two arguments
auto rr = c1(2, 5); // partial application and call `foo(15,20,2,5)`
auto c0 = curry(foo); // currying
auto c1 = c0(15, 20); // partial application of two arguments
auto rr = c1(2, 5); // partial application and call `foo(15,20,2,5)`
// output: 42
std::cout << rr << std::endl;
@@ -88,12 +88,12 @@ auto boo = [](int v1, int v2) {
};
auto foo = [boo](int v1, int v2) {
return kari::curry(boo, v1 + v2);
return curry(boo, v1 + v2);
};
auto c0 = kari::curry(foo)(38,3,1);
auto c1 = kari::curry(foo)(38,3)(1);
auto c2 = kari::curry(foo)(38)(3,1);
auto c0 = curry(foo)(38,3,1);
auto c1 = curry(foo)(38,3)(1);
auto c2 = curry(foo)(38)(3,1);
// output: 42,42,42
std::cout << c0 << "," << c1 << "," << c2 << std::endl;
@@ -110,8 +110,8 @@ struct Foo {
}
} foo;
auto c0 = kari::curry(&Foo::addV);
auto c1 = kari::curry(&Foo::v);
auto c0 = curry(&Foo::addV);
auto c1 = curry(&Foo::v);
auto r0 = c0(std::ref(foo))(2);
auto r1 = c1(foo);
@@ -123,7 +123,7 @@ std::cout << r0 << "," << r1 << std::endl;
## API
```cpp
namespace kari {
namespace kari_hpp {
template < typename F, typename... Args >
constexpr decltype(auto) curry(F&& f, Args&&... args) const;
@@ -149,18 +149,18 @@ namespace kari {
---
### `kari::curry(F&& f, Args&&... args)`
### `kari_hpp::curry(F&& f, Args&&... args)`
Returns a curried function **`f`** or copy the function result with **`args`** arguments.
---
### `kari::curryV(F&& f, Args&&... args)`
### `kari_hpp::curryV(F&& f, Args&&... args)`
Allows carrying variadic functions.
```cpp
auto c0 = kari::curryV(std::printf, "%d + %d = %d");
auto c0 = curryV(std::printf, "%d + %d = %d");
auto c1 = c0(37, 5);
auto c2 = c1(42);
@@ -170,20 +170,20 @@ c2(); // output: 37 + 5 = 42
---
### `kari::curryN(F&& f, Args&&... args)`
### `kari_hpp::curryN(F&& f, Args&&... args)`
Allows carrying variadic functions for **`N`** arguments.
```cpp
char buffer[256] = {'\0'};
auto c = kari::curryN<3>(std::snprintf, buffer, 256, "%d + %d = %d");
auto c = curryN<3>(std::snprintf, buffer, 256, "%d + %d = %d");
c(37, 5, 42);
std::cout << buffer << std::endl; // output: 37 + 5 = 42
```
---
### `kari::is_curried<F>, kari::is_curried_v<F>`
### `kari_hpp::is_curried<F>, kari_hpp::is_curried_v<F>`
Checks whether F is a curried function type.
@@ -208,7 +208,7 @@ std::cout
---
### `kari::curry_t::operator()(As&&... as)`
### `kari_hpp::curry_t::operator()(As&&... as)`
Calling operator of curried function for partial application or full application. Returns a new curried function with added new arguments or copy of the function result.
@@ -217,7 +217,7 @@ int foo(int v1, int v2, int v3, int v4) {
return v1 + v2 + v3 + v4;
}
auto c0 = kari::curry(foo); // currying
auto c0 = curry(foo); // currying
auto c1 = c0(15, 20); // partial application
auto rr = c2(2, 5); // function call - foo(15,20,2,5)
@@ -232,7 +232,7 @@ std::cout << rr << std::endl;
### Section of operators
```cpp
using namespace kari::underscore;
using namespace underscore;
std::vector<int> v{1,2,3,4};
// result: 10
@@ -250,7 +250,7 @@ std::transform(v.begin(), v.end(), v.begin(), -_);
#### Pipe operator
```cpp
using namespace kari::underscore;
using namespace underscore;
auto r0 = (_*2) | (_+2) | 4; // (4 * 2) + 2 = 10
auto r1 = 4 | (_*2) | (_+2); // (4 * 2 + 2) = 10
@@ -262,7 +262,7 @@ std::cout << r0, << "," << r1 << std::endl;
#### Compose operator
```cpp
using namespace kari::underscore;
using namespace underscore;
auto r0 = (_*2) * (_+2) * 4; // (4 + 2) * 2 = 12
auto r1 = 4 * (_*2) * (_+2); // (4 * 2 + 2) = 10
@@ -274,7 +274,7 @@ std::cout << r0, << "," << r1 << std::endl;
### Point-free style for Haskell maniacs
```cpp
using namespace kari::underscore;
using namespace underscore;
// (. (+2)) (*2) $ 10 == 24 // haskell analog
auto r0 = (_*(_+2))(_*2) * 10;

View File

@@ -18,7 +18,7 @@
#define KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(...) \
noexcept(noexcept(__VA_ARGS__)) -> decltype (__VA_ARGS__) { return __VA_ARGS__; }
namespace kari
namespace kari_hpp
{
template < std::size_t N, typename F, typename... Args >
struct curry_t;

View File

@@ -14,7 +14,7 @@
#define KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(...) \
noexcept(noexcept(__VA_ARGS__)) -> decltype (__VA_ARGS__) { return __VA_ARGS__; }
namespace kari
namespace kari_hpp
{
//
// fid
@@ -160,74 +160,71 @@ namespace kari
std::forward<F>(f)(std::forward<A>(a)))
}
namespace kari
namespace kari_hpp::underscore
{
namespace underscore
{
struct us_t {};
inline constexpr us_t _{};
struct us_t {};
inline constexpr us_t _{};
//
// is_underscore, is_underscore_v
//
//
// is_underscore, is_underscore_v
//
template < typename T >
struct is_underscore
: std::bool_constant<std::is_same_v<us_t, std::remove_cv_t<T>>> {};
template < typename T >
struct is_underscore
: std::bool_constant<std::is_same_v<us_t, std::remove_cv_t<T>>> {};
template < typename T >
inline constexpr bool is_underscore_v = is_underscore<T>::value;
template < typename T >
inline constexpr bool is_underscore_v = is_underscore<T>::value;
//
// unary operators
//
//
// unary operators
//
#define KARI_HPP_DEFINE_UNDERSCORE_UNARY_OP(op, func) \
constexpr auto operator op (us_t) KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(curry(func))
#define KARI_HPP_DEFINE_UNDERSCORE_UNARY_OP(op, func) \
constexpr auto operator op (us_t) KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(curry(func))
KARI_HPP_DEFINE_UNDERSCORE_UNARY_OP(-, std::negate<>())
KARI_HPP_DEFINE_UNDERSCORE_UNARY_OP(~, std::bit_not<>())
KARI_HPP_DEFINE_UNDERSCORE_UNARY_OP(!, std::logical_not<>())
#undef KARI_HPP_DEFINE_UNDERSCORE_UNARY_OP
KARI_HPP_DEFINE_UNDERSCORE_UNARY_OP(-, std::negate<>())
KARI_HPP_DEFINE_UNDERSCORE_UNARY_OP(~, std::bit_not<>())
KARI_HPP_DEFINE_UNDERSCORE_UNARY_OP(!, std::logical_not<>())
#undef KARI_HPP_DEFINE_UNDERSCORE_UNARY_OP
//
// binary operators
//
//
// binary operators
//
#define KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(op, func) \
constexpr auto operator op (us_t, us_t) \
KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(curry(func)) \
\
template < typename A, typename std::enable_if_t<!is_underscore_v<std::decay_t<A>>, int> = 0 > \
constexpr auto operator op (A&& a, us_t) \
KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(curry(func, std::forward<A>(a))) \
\
template < typename B, typename std::enable_if_t<!is_underscore_v<std::decay_t<B>>, int> = 0 > \
constexpr auto operator op (us_t, B&& b) \
KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(fflip(func, std::forward<B>(b)))
#define KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(op, func) \
constexpr auto operator op (us_t, us_t) \
KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(curry(func)) \
\
template < typename A, typename std::enable_if_t<!is_underscore_v<std::decay_t<A>>, int> = 0 > \
constexpr auto operator op (A&& a, us_t) \
KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(curry(func, std::forward<A>(a))) \
\
template < typename B, typename std::enable_if_t<!is_underscore_v<std::decay_t<B>>, int> = 0 > \
constexpr auto operator op (us_t, B&& b) \
KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(fflip(func, std::forward<B>(b)))
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(+ , std::plus<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(- , std::minus<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(* , std::multiplies<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(/ , std::divides<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(% , std::modulus<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(+ , std::plus<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(- , std::minus<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(* , std::multiplies<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(/ , std::divides<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(% , std::modulus<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(< , std::less<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(> , std::greater<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(<=, std::less_equal<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(>=, std::greater_equal<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(< , std::less<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(> , std::greater<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(<=, std::less_equal<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(>=, std::greater_equal<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(==, std::equal_to<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(!=, std::not_equal_to<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(==, std::equal_to<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(!=, std::not_equal_to<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(| , std::bit_or<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(& , std::bit_and<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(^ , std::bit_xor<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(| , std::bit_or<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(& , std::bit_and<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(^ , std::bit_xor<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(||, std::logical_or<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(&&, std::logical_and<>())
#undef KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP
}
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(||, std::logical_or<>())
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(&&, std::logical_and<>())
#undef KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP
}
#undef KARI_HPP_NOEXCEPT_RETURN

View File

@@ -7,11 +7,11 @@
#include <kari.hpp/kari_ext.hpp>
#include "doctest/doctest.hpp"
using namespace kari;
using namespace kari_hpp;
TEST_CASE("kari_feature") {
SUBCASE("underscore") {
using namespace kari::underscore;
using namespace underscore;
REQUIRE((-_)(40) == -40);
REQUIRE((_ + 40)(2) == 42);

View File

@@ -192,7 +192,7 @@ namespace
}
}
using namespace kari;
using namespace kari_hpp;
TEST_CASE("kari_feature") {
SUBCASE("ref_functor") {
@@ -503,13 +503,13 @@ TEST_CASE("kari") {
}
{
char buffer[256] = {'\0'};
auto c = kari::curryV(std::snprintf, buffer, 256, "%d + %d = %d");
auto c = curryV(std::snprintf, buffer, 256, "%d + %d = %d");
c(37, 5, 42)();
REQUIRE(std::strcmp("37 + 5 = 42", buffer) == 0);
}
{
char buffer[256] = {'\0'};
auto c = kari::curryN<3>(std::snprintf, buffer, 256, "%d + %d = %d");
auto c = curryN<3>(std::snprintf, buffer, 256, "%d + %d = %d");
c(37, 5, 42);
REQUIRE(std::strcmp("37 + 5 = 42", buffer) == 0);
}