Browse Source

refactoring, curry and compose work

master
Ludovic 'Archivist' Lagouardette 6 years ago
parent
commit
3a3e788fd2
6 changed files with 69 additions and 19 deletions
  1. +1
    -1
      Makefile
  2. +11
    -9
      include/combinations/compose.hpp
  3. +6
    -6
      include/combinations/curry.hpp
  4. +10
    -1
      include/combinations/function.hpp
  5. +15
    -2
      tests/compose.cpp
  6. +26
    -0
      tests/curry.cpp

+ 1
- 1
Makefile View File

@ -3,4 +3,4 @@
all: test
test:
g++ -Iinclude --std=c++17 -o test tests/*.cpp
g++ -g -Iinclude --std=c++17 -o test tests/*.cpp

+ 11
- 9
include/combinations/compose.hpp View File

@ -2,18 +2,14 @@
#include "combinations/tuple.hpp"
namespace cl{
struct sigil_t{
constexpr sigil_t(){}
};
sigil_t sigil;
template<typename a, typename ...c>
class compose{
class compose_t{
// f . next
a f;
compose<c...> next;
compose_t<c...> next;
public:
compose(a fn1, c ...oth)
compose_t(a fn1, c ...oth)
: f{fn1}
, next{oth...}
{}
@ -25,11 +21,11 @@ namespace cl{
};
template<typename a>
class compose<a>{
class compose_t<a>{
// f . g
a f;
public:
compose(a fn1)
compose_t(a fn1)
: f{fn1}
{}
template<typename ...t>
@ -39,6 +35,12 @@ namespace cl{
};
template<typename a, typename ...c>
auto compose(a fn1, c ...oth)
{
return function(compose_t(fn1,oth...));
}
template<typename a, typename b>
auto operator*(a fn1, b fn2)
{

+ 6
- 6
include/combinations/curry.hpp View File

@ -2,15 +2,15 @@
namespace cl
{
template<typename a, typename fn>
class curry
template<typename fn, typename a>
class curry_t
{
a param;
fn call;
a param;
public:
curry(a p, fn func)
: param{p}
, call{func}
curry_t(fn func, a p)
: call{func}
, param{p}
{}
template<typename ...b>
auto operator()(b ...Args){

+ 10
- 1
include/combinations/function.hpp View File

@ -3,6 +3,9 @@
namespace cl
{
template<typename fn, typename a>
auto curry(fn func, a p);
template <typename a>
class function{
a me;
@ -16,10 +19,16 @@ namespace cl
}
template<typename cur, typename ...b>
auto operator()(cur arg){
return curry(arg, me);
return curry(me, arg);
}
};
template<typename fn, typename a>
auto curry(fn func, a p)
{
return function(curry_t(func, p));
}
template<typename invokable, typename ret, typename ...Args>
ret invoke(invokable fn, Args ...args)
{

+ 15
- 2
tests/compose.cpp View File

@ -8,8 +8,21 @@ struct test_001{
[](int a)->int{return a+2;},
[](int a)->int{return a*2;}
);
assert(fn(1)==6);
assert(fn(1)p">()==6);
}
};
test_001 run_001;
test_001 run_001;
struct test_002{
test_002()
{
using namespace cl;
auto fn =
[](int a)->int{return a+2;}
* [](int a)->int{return a*2;};
assert(fn(2)()==8);
}
};
test_002 run_002;

+ 26
- 0
tests/curry.cpp View File

@ -0,0 +1,26 @@
#include "2CL.hpp"
#include <assert.h>
struct test_003{
test_003()
{
auto fn = cl::curry(
[](int a,int b)->int{return a+b;},
11
);
assert(fn(1)()==12);
}
};
test_003 run_003;
struct test_004{
test_004()
{
using namespace cl;
auto fn = function([](int a, int b)->int{return a+b;});
assert(fn(2)(7)()==9);
}
};
test_004 run_004;

Loading…
Cancel
Save