mirror of
https://github.com/BlackMATov/kari.hpp.git
synced 2025-12-17 00:16:52 +07:00
rename kari namespace to kari_hpp
This commit is contained in:
42
README.md
42
README.md
@@ -56,7 +56,7 @@ auto foo = [](int v1, int v2, int v3) {
|
|||||||
return v1 + v2 + v3;
|
return v1 + v2 + v3;
|
||||||
};
|
};
|
||||||
|
|
||||||
auto c0 = kari::curry(foo); // currying of `foo` function
|
auto c0 = curry(foo); // currying of `foo` function
|
||||||
auto c1 = c0(10); // apply to first argument
|
auto c1 = c0(10); // apply to first argument
|
||||||
auto c2 = c1(20); // apply to second argument
|
auto c2 = c1(20); // apply to second argument
|
||||||
auto rr = c2(12); // apply to third argument and call the `foo` function
|
auto rr = c2(12); // apply to third argument and call the `foo` function
|
||||||
@@ -72,7 +72,7 @@ auto foo = [](int v1, int v2, int v3, int v4) {
|
|||||||
return v1 + v2 + v3 + v4;
|
return v1 + v2 + v3 + v4;
|
||||||
};
|
};
|
||||||
|
|
||||||
auto c0 = kari::curry(foo); // currying
|
auto c0 = curry(foo); // currying
|
||||||
auto c1 = c0(15, 20); // partial application of two arguments
|
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 rr = c1(2, 5); // partial application and call `foo(15,20,2,5)`
|
||||||
|
|
||||||
@@ -88,12 +88,12 @@ auto boo = [](int v1, int v2) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
auto foo = [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 c0 = curry(foo)(38,3,1);
|
||||||
auto c1 = kari::curry(foo)(38,3)(1);
|
auto c1 = curry(foo)(38,3)(1);
|
||||||
auto c2 = kari::curry(foo)(38)(3,1);
|
auto c2 = curry(foo)(38)(3,1);
|
||||||
|
|
||||||
// output: 42,42,42
|
// output: 42,42,42
|
||||||
std::cout << c0 << "," << c1 << "," << c2 << std::endl;
|
std::cout << c0 << "," << c1 << "," << c2 << std::endl;
|
||||||
@@ -110,8 +110,8 @@ struct Foo {
|
|||||||
}
|
}
|
||||||
} foo;
|
} foo;
|
||||||
|
|
||||||
auto c0 = kari::curry(&Foo::addV);
|
auto c0 = curry(&Foo::addV);
|
||||||
auto c1 = kari::curry(&Foo::v);
|
auto c1 = curry(&Foo::v);
|
||||||
|
|
||||||
auto r0 = c0(std::ref(foo))(2);
|
auto r0 = c0(std::ref(foo))(2);
|
||||||
auto r1 = c1(foo);
|
auto r1 = c1(foo);
|
||||||
@@ -123,7 +123,7 @@ std::cout << r0 << "," << r1 << std::endl;
|
|||||||
## API
|
## API
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
namespace kari {
|
namespace kari_hpp {
|
||||||
template < typename F, typename... Args >
|
template < typename F, typename... Args >
|
||||||
constexpr decltype(auto) curry(F&& f, Args&&... args) const;
|
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.
|
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.
|
Allows carrying variadic functions.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
auto c0 = kari::curryV(std::printf, "%d + %d = %d");
|
auto c0 = curryV(std::printf, "%d + %d = %d");
|
||||||
auto c1 = c0(37, 5);
|
auto c1 = c0(37, 5);
|
||||||
auto c2 = c1(42);
|
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.
|
Allows carrying variadic functions for **`N`** arguments.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
char buffer[256] = {'\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);
|
c(37, 5, 42);
|
||||||
std::cout << buffer << std::endl; // output: 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.
|
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.
|
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;
|
return v1 + v2 + v3 + v4;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto c0 = kari::curry(foo); // currying
|
auto c0 = curry(foo); // currying
|
||||||
auto c1 = c0(15, 20); // partial application
|
auto c1 = c0(15, 20); // partial application
|
||||||
auto rr = c2(2, 5); // function call - foo(15,20,2,5)
|
auto rr = c2(2, 5); // function call - foo(15,20,2,5)
|
||||||
|
|
||||||
@@ -232,7 +232,7 @@ std::cout << rr << std::endl;
|
|||||||
### Section of operators
|
### Section of operators
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
using namespace kari::underscore;
|
using namespace underscore;
|
||||||
std::vector<int> v{1,2,3,4};
|
std::vector<int> v{1,2,3,4};
|
||||||
|
|
||||||
// result: 10
|
// result: 10
|
||||||
@@ -250,7 +250,7 @@ std::transform(v.begin(), v.end(), v.begin(), -_);
|
|||||||
#### Pipe operator
|
#### Pipe operator
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
using namespace kari::underscore;
|
using namespace underscore;
|
||||||
|
|
||||||
auto r0 = (_*2) | (_+2) | 4; // (4 * 2) + 2 = 10
|
auto r0 = (_*2) | (_+2) | 4; // (4 * 2) + 2 = 10
|
||||||
auto r1 = 4 | (_*2) | (_+2); // (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
|
#### Compose operator
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
using namespace kari::underscore;
|
using namespace underscore;
|
||||||
|
|
||||||
auto r0 = (_*2) * (_+2) * 4; // (4 + 2) * 2 = 12
|
auto r0 = (_*2) * (_+2) * 4; // (4 + 2) * 2 = 12
|
||||||
auto r1 = 4 * (_*2) * (_+2); // (4 * 2 + 2) = 10
|
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
|
### Point-free style for Haskell maniacs
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
using namespace kari::underscore;
|
using namespace underscore;
|
||||||
|
|
||||||
// (. (+2)) (*2) $ 10 == 24 // haskell analog
|
// (. (+2)) (*2) $ 10 == 24 // haskell analog
|
||||||
auto r0 = (_*(_+2))(_*2) * 10;
|
auto r0 = (_*(_+2))(_*2) * 10;
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
#define KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(...) \
|
#define KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(...) \
|
||||||
noexcept(noexcept(__VA_ARGS__)) -> decltype (__VA_ARGS__) { return __VA_ARGS__; }
|
noexcept(noexcept(__VA_ARGS__)) -> decltype (__VA_ARGS__) { return __VA_ARGS__; }
|
||||||
|
|
||||||
namespace kari
|
namespace kari_hpp
|
||||||
{
|
{
|
||||||
template < std::size_t N, typename F, typename... Args >
|
template < std::size_t N, typename F, typename... Args >
|
||||||
struct curry_t;
|
struct curry_t;
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
#define KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(...) \
|
#define KARI_HPP_NOEXCEPT_DECLTYPE_RETURN(...) \
|
||||||
noexcept(noexcept(__VA_ARGS__)) -> decltype (__VA_ARGS__) { return __VA_ARGS__; }
|
noexcept(noexcept(__VA_ARGS__)) -> decltype (__VA_ARGS__) { return __VA_ARGS__; }
|
||||||
|
|
||||||
namespace kari
|
namespace kari_hpp
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// fid
|
// fid
|
||||||
@@ -160,10 +160,8 @@ namespace kari
|
|||||||
std::forward<F>(f)(std::forward<A>(a)))
|
std::forward<F>(f)(std::forward<A>(a)))
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace kari
|
namespace kari_hpp::underscore
|
||||||
{
|
{
|
||||||
namespace underscore
|
|
||||||
{
|
|
||||||
struct us_t {};
|
struct us_t {};
|
||||||
inline constexpr us_t _{};
|
inline constexpr us_t _{};
|
||||||
|
|
||||||
@@ -227,7 +225,6 @@ namespace kari
|
|||||||
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(||, std::logical_or<>())
|
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(||, std::logical_or<>())
|
||||||
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(&&, std::logical_and<>())
|
KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP(&&, std::logical_and<>())
|
||||||
#undef KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP
|
#undef KARI_HPP_DEFINE_UNDERSCORE_BINARY_OP
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef KARI_HPP_NOEXCEPT_RETURN
|
#undef KARI_HPP_NOEXCEPT_RETURN
|
||||||
|
|||||||
@@ -7,11 +7,11 @@
|
|||||||
#include <kari.hpp/kari_ext.hpp>
|
#include <kari.hpp/kari_ext.hpp>
|
||||||
#include "doctest/doctest.hpp"
|
#include "doctest/doctest.hpp"
|
||||||
|
|
||||||
using namespace kari;
|
using namespace kari_hpp;
|
||||||
|
|
||||||
TEST_CASE("kari_feature") {
|
TEST_CASE("kari_feature") {
|
||||||
SUBCASE("underscore") {
|
SUBCASE("underscore") {
|
||||||
using namespace kari::underscore;
|
using namespace underscore;
|
||||||
REQUIRE((-_)(40) == -40);
|
REQUIRE((-_)(40) == -40);
|
||||||
|
|
||||||
REQUIRE((_ + 40)(2) == 42);
|
REQUIRE((_ + 40)(2) == 42);
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ namespace
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
using namespace kari;
|
using namespace kari_hpp;
|
||||||
|
|
||||||
TEST_CASE("kari_feature") {
|
TEST_CASE("kari_feature") {
|
||||||
SUBCASE("ref_functor") {
|
SUBCASE("ref_functor") {
|
||||||
@@ -503,13 +503,13 @@ TEST_CASE("kari") {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
char buffer[256] = {'\0'};
|
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)();
|
c(37, 5, 42)();
|
||||||
REQUIRE(std::strcmp("37 + 5 = 42", buffer) == 0);
|
REQUIRE(std::strcmp("37 + 5 = 42", buffer) == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
char buffer[256] = {'\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);
|
c(37, 5, 42);
|
||||||
REQUIRE(std::strcmp("37 + 5 = 42", buffer) == 0);
|
REQUIRE(std::strcmp("37 + 5 = 42", buffer) == 0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user