initial commit
This commit is contained in:
commit
57d43acd16
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
.cache/
|
||||
compile_commands.json
|
8
CMakeLists.txt
Normal file
8
CMakeLists.txt
Normal file
|
@ -0,0 +1,8 @@
|
|||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
project(templates CXX)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(templates
|
||||
src/main.cpp
|
||||
)
|
14
src/Data.hpp
Normal file
14
src/Data.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
template<typename Type, Type Value>
|
||||
struct Data {
|
||||
using type = Type;
|
||||
static constexpr Type value = Value;
|
||||
};
|
||||
|
||||
template<int64_t Value>
|
||||
struct Number :
|
||||
public Data<int64_t, Value>
|
||||
{};
|
||||
|
35
src/Lambda.hpp
Normal file
35
src/Lambda.hpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include "Data.hpp"
|
||||
|
||||
template <typename Return, typename... Parameters>
|
||||
struct Lambda {
|
||||
using returnType = Return;
|
||||
// using paramType = Parameter;
|
||||
};
|
||||
|
||||
template <typename X>
|
||||
struct Identity :
|
||||
public Lambda<typename X::type, typename X::type>
|
||||
{
|
||||
using value = X;
|
||||
};
|
||||
|
||||
template <typename X>
|
||||
struct NumericIncrement :
|
||||
public Lambda<typename X::type, typename X::type>
|
||||
{
|
||||
using value = Number<X::value + 1>;
|
||||
};
|
||||
|
||||
template <typename X>
|
||||
struct NumericDouble :
|
||||
public Lambda<typename X::type, typename X::type>
|
||||
{
|
||||
using value = Number<X::value * 2>;
|
||||
};
|
||||
|
||||
template <typename X, typename Y>
|
||||
struct NumericAdd :
|
||||
public Lambda<typename X::type, typename X::type, typename Y::type>
|
||||
{
|
||||
using value = Number<X::value + Y::value>;
|
||||
};
|
75
src/List.hpp
Normal file
75
src/List.hpp
Normal file
|
@ -0,0 +1,75 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include "Lambda.hpp"
|
||||
|
||||
template <typename Head, typename Cons>
|
||||
struct List {
|
||||
using type = typename Head::type;
|
||||
using value = Head;
|
||||
using cons = Cons;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct List<void, void> {
|
||||
using type = void;
|
||||
using value = void;
|
||||
};
|
||||
|
||||
template <typename Head>
|
||||
struct List<Head, void> {
|
||||
using type = typename Head::type;
|
||||
using value = Head;
|
||||
using cons = ::List<void, void>;
|
||||
};
|
||||
|
||||
template<typename Head, typename... Cons>
|
||||
struct ListConstructor {
|
||||
using value = ::List<Head, typename ListConstructor<Cons...>::value>;
|
||||
};
|
||||
|
||||
template<typename Head>
|
||||
struct ListConstructor<Head> {
|
||||
using value = ::List<Head, void>;
|
||||
};
|
||||
|
||||
template<typename List, uint64_t N>
|
||||
struct List_Nth_Element {
|
||||
using value = typename List_Nth_Element<typename List::cons, N - 1>::value;
|
||||
};
|
||||
|
||||
template<typename List>
|
||||
struct List_Nth_Element<List, 0> {
|
||||
using value = typename List::value;
|
||||
};
|
||||
|
||||
|
||||
template<template <typename, typename> typename Func, typename Init, typename List, bool = std::is_same<typename List::type, void>::value>
|
||||
struct Fold :
|
||||
public Lambda<Init, Func<typename List::type, Init>, Init, List>
|
||||
{
|
||||
using value = Init;
|
||||
};
|
||||
|
||||
template<template <typename, typename> typename Func, typename Init, typename List>
|
||||
struct Fold<Func, Init, List, false> :
|
||||
public Lambda<Init, Func<typename List::type, Init>, Init, List>
|
||||
{
|
||||
using value = Func<typename List::value, typename Fold<Func, Init, typename List::cons>::value>::value;
|
||||
};
|
||||
|
||||
|
||||
template<template <typename> typename Func, typename List, bool = std::is_same<typename List::type, void>::value>
|
||||
struct Map :
|
||||
public Lambda<List, Func<typename List::type>, List>
|
||||
{
|
||||
using value = ::List<void, void>;
|
||||
};
|
||||
|
||||
template<template <typename> typename Func, typename List>
|
||||
struct Map<Func, List, false> :
|
||||
public Lambda<List, Func<typename List::type>, List>
|
||||
{
|
||||
using value = ::List<typename Func<typename List::value>::value, typename Map<Func, typename List::cons>::value>;
|
||||
};
|
46
src/main.cpp
Normal file
46
src/main.cpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include "Data.hpp"
|
||||
#include "List.hpp"
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
|
||||
constexpr uint32_t add(uint32_t left, uint32_t right) {
|
||||
return left + right;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
using list = ListConstructor<Number<3>, Number<7>, Number<4>, Number<12>>::value;
|
||||
using result = Fold<NumericAdd, Number<0>, typename Map<NumericDouble, list>::value>::value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// using first = List_Nth_Element<list, 0>::value;
|
||||
// using second = List_Nth_Element<list, 1>::value;
|
||||
// using third = List_Nth_Element<list, 2>::value;
|
||||
// using fourth = List_Nth_Element<list, 3>::value;
|
||||
// // using fifth = List_Nth_Element<list, 4>::value;
|
||||
//
|
||||
// using incremented = NumericIncrement<first>::value;
|
||||
//
|
||||
// using addition = NumericAdd<second, fourth>::value;
|
||||
//
|
||||
// using sum = Fold<NumericAdd, Number<0>, list>::value;
|
||||
//
|
||||
//
|
||||
// return 0;
|
||||
// }
|
||||
//
|
Loading…
Reference in a new issue