Browse Source

Compose works (tested)

master
Ludovic 'Archivist' Lagouardette 5 years ago
commit
aacdc171ae
11 changed files with 156 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +6
    -0
      Makefile
  3. +0
    -0
      README.md
  4. +2
    -0
      include/2CL.hpp
  5. +5
    -0
      include/combinations/all.hpp
  6. +47
    -0
      include/combinations/compose.hpp
  7. +20
    -0
      include/combinations/curry.hpp
  8. +28
    -0
      include/combinations/function.hpp
  9. +28
    -0
      include/combinations/tuple.hpp
  10. +15
    -0
      tests/compose.cpp
  11. +4
    -0
      tests/void.cpp

+ 1
- 0
.gitignore View File

@ -0,0 +1 @@
test

+ 6
- 0
Makefile View File

@ -0,0 +1,6 @@
all: test
test:
g++ -Iinclude --std=c++17 -o test tests/*.cpp

+ 0
- 0
README.md View File


+ 2
- 0
include/2CL.hpp View File

@ -0,0 +1,2 @@
#pragma once
#include "combinations/all.hpp"

+ 5
- 0
include/combinations/all.hpp View File

@ -0,0 +1,5 @@
#pragma once
#include "combinations/function.hpp"
#include "combinations/compose.hpp"
#include "combinations/tuple.hpp"
#include "combinations/curry.hpp"

+ 47
- 0
include/combinations/compose.hpp View File

@ -0,0 +1,47 @@
#pragma once
#include "combinations/tuple.hpp"
namespace cl{
struct sigil_t{
constexpr sigil_t(){}
};
sigil_t sigil;
template<typename a, typename ...c>
class compose{
// f . next
a f;
compose<c...> next;
public:
compose(a fn1, c ...oth)
: f{fn1}
, next{oth...}
{}
template<typename ...t>
auto operator()(t ...Args){
return next(f(Args...));
}
};
template<typename a>
class compose<a>{
// f . g
a f;
public:
compose(a fn1)
: f{fn1}
{}
template<typename ...t>
auto operator()(t ...Args){
return f(Args...);
}
};
template<typename a, typename b>
auto operator*(a fn1, b fn2)
{
return compose(fn1, fn2);
}
}

+ 20
- 0
include/combinations/curry.hpp View File

@ -0,0 +1,20 @@
#pragma once
namespace cl
{
template<typename a, typename fn>
class curry
{
a param;
fn call;
public:
curry(a p, fn func)
: param{p}
, call{func}
{}
template<typename ...b>
auto operator()(b ...Args){
return call(param, Args...);
}
};
}

+ 28
- 0
include/combinations/function.hpp View File

@ -0,0 +1,28 @@
#pragma once
#include "combinations/curry.hpp"
namespace cl
{
template <typename a>
class function{
a me;
public:
function(a value)
: me(value)
{}
template<typename ...b>
auto operator()(b ...Args){
return me(Args...);
}
template<typename cur, typename ...b>
auto operator()(cur arg){
return curry(arg, me);
}
};
template<typename invokable, typename ret, typename ...Args>
ret invoke(invokable fn, Args ...args)
{
return fn(args...);
}
}

+ 28
- 0
include/combinations/tuple.hpp View File

@ -0,0 +1,28 @@
#pragma once
#include <stddef.h>
namespace cl
{
template<typename a, typename ...b>
class tuple{
a _mine;
tuple<b...> _child;
template<size_t idx, a, b...>
friend a get();
public:
tuple(a mine, b ...child)
: _mine(mine)
, _child(child...)
{}
};
template<size_t idx, typename ...a>
constexpr auto get(tuple<a...> v)
{
if constexpr (idx!=0)
return get<idx-1>(v._child);
else
return v._mine;
}
}

+ 15
- 0
tests/compose.cpp View File

@ -0,0 +1,15 @@
#include "2CL.hpp"
#include <assert.h>
struct test_001{
test_001()
{
auto fn = cl::compose(
[](int a)->int{return a+2;},
[](int a)->int{return a*2;}
);
assert(fn(1)==6);
}
};
test_001 run_001;

+ 4
- 0
tests/void.cpp View File

@ -0,0 +1,4 @@
int main()
{
return 0;
}

Loading…
Cancel
Save