From f81cd736e00c28cf24412a4099bae08ff2e6c493 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Mon, 16 Feb 2015 17:35:38 +0100 Subject: Unified `Iota` and `MakeList` using nested structure generator * `detail::generate_nested_structure` offers a higher order nested structure constructor for procedural list generation * renamed `Fold` implementation details --- README.md | 20 +++++------ src/function/detail/apply.h | 11 ++---- .../generator/detail/generate_nested_structure.h | 39 ++++++++++++++++++++++ src/list/generator/iota.h | 38 +++++---------------- src/list/generator/make_list.h | 34 +++++-------------- src/list/operation/higher/fold.h | 8 ++--- src/list/operation/nth.h | 3 +- 7 files changed, 74 insertions(+), 79 deletions(-) create mode 100644 src/list/generator/detail/generate_nested_structure.h diff --git a/README.md b/README.md index 50d6dff..ba07515 100644 --- a/README.md +++ b/README.md @@ -6,17 +6,15 @@ This library is a expanded reimplementation of my previous attempt at this probl ## Example -```cpp -// λ (length (filter odd? (list 1 2 3))) -// 2 - -const std::size_t count = tav::Length< - tav::Filter< - tav::Odd, - tav::List, tav::Int<2>, tav::Int<3>>::type - >::type ->::type::value; -``` + // λ (length (filter odd? (list 1 2 3))) + // 2 + + const std::size_t count = tav::Length< + tav::Filter< + tav::Odd, + tav::List, tav::Int<2>, tav::Int<3>> + > + >::value; ## Current features diff --git a/src/function/detail/apply.h b/src/function/detail/apply.h index 3df80ef..aca0af4 100644 --- a/src/function/detail/apply.h +++ b/src/function/detail/apply.h @@ -13,17 +13,12 @@ namespace detail { struct placeholder_tag { }; template -using is_placeholder = Boolean< - std::is_base_of::value ->; +using is_placeholder = Eval>; template struct placeholder : placeholder_tag { }; -template < - typename Partials, - typename Argument -> +template struct resolve_placeholder { typedef Argument type; }; @@ -33,7 +28,7 @@ template < int Index > struct resolve_placeholder> { - typedef tav::Nth, Partials> type; + typedef Nth, Partials> type; }; template diff --git a/src/list/generator/detail/generate_nested_structure.h b/src/list/generator/detail/generate_nested_structure.h new file mode 100644 index 0000000..84db78d --- /dev/null +++ b/src/list/generator/detail/generate_nested_structure.h @@ -0,0 +1,39 @@ +#ifndef TYPEASVALUE_SRC_LIST_GENERATOR_DETAIL_GENERATE_NESTED_STRUCTURE_H_ +#define TYPEASVALUE_SRC_LIST_GENERATOR_DETAIL_GENERATE_NESTED_STRUCTURE_H_ + +namespace tav { + +namespace detail { + +template < + template class Structure, + template class Generator, + typename Initial, + typename Count +> +struct generate_nested_structure { + typedef Structure< + Initial, + Eval, + Substract> + >> + > type; +}; + +template < + template class Structure, + template class Generator, + typename Initial +> +struct generate_nested_structure> { + typedef void type; +}; + +} + +} + +#endif // TYPEASVALUE_SRC_LIST_GENERATOR_DETAIL_GENERATE_NESTED_STRUCTURE_H_ diff --git a/src/list/generator/iota.h b/src/list/generator/iota.h index 73b7cc8..1def67e 100644 --- a/src/list/generator/iota.h +++ b/src/list/generator/iota.h @@ -1,44 +1,24 @@ #ifndef TYPEASVALUE_SRC_LIST_GENERATOR_IOTA_H_ #define TYPEASVALUE_SRC_LIST_GENERATOR_IOTA_H_ +#include "pair.h" #include "operation/math.h" +#include "function/apply.h" +#include "detail/generate_nested_structure.h" namespace tav { -namespace detail { - -template < - typename Count, - typename Initial, - typename Step -> -struct Iota { - typedef Cons< - Initial, - Eval>, - Add, - Step - >> - > type; -}; - -template < - typename Initial, - typename Step -> -struct Iota, Initial, Step> { - typedef Cons type; -}; - -} - template < typename Count, typename Initial, typename Step > -using Iota = Eval>; +using Iota = Eval::template function, + Initial, + Count +>>; } diff --git a/src/list/generator/make_list.h b/src/list/generator/make_list.h index 863dce2..ec49651 100644 --- a/src/list/generator/make_list.h +++ b/src/list/generator/make_list.h @@ -1,39 +1,21 @@ #ifndef TYPEASVALUE_SRC_LIST_GENERATOR_MAKE_LIST_H_ #define TYPEASVALUE_SRC_LIST_GENERATOR_MAKE_LIST_H_ -#include "list/cons.h" -#include "operation/math.h" +#include "pair.h" +#include "detail/generate_nested_structure.h" namespace tav { -namespace detail { - -template < - typename Count, - typename Element -> -struct MakeList { - typedef Cons< - Element, - Eval>, - Element - >> - > type; -}; - -template -struct MakeList, Element> { - typedef Cons type; -}; - -} - template < typename Count, typename Element > -using MakeList = Eval>; +using MakeList = Eval>; } diff --git a/src/list/operation/higher/fold.h b/src/list/operation/higher/fold.h index 7364fe5..3f39b40 100644 --- a/src/list/operation/higher/fold.h +++ b/src/list/operation/higher/fold.h @@ -10,10 +10,10 @@ template < typename Initial, typename Current > -struct Fold { +struct fold_pair { typedef Function< Head, - Eval>> + Eval>> > type; }; @@ -21,7 +21,7 @@ template < template class Function, typename Initial > -struct Fold { +struct fold_pair { typedef Initial type; }; @@ -32,7 +32,7 @@ template < typename Initial, typename Current > -using Fold = Eval>; +using Fold = Eval>; } diff --git a/src/list/operation/nth.h b/src/list/operation/nth.h index 0a56ba3..1ffb1f0 100644 --- a/src/list/operation/nth.h +++ b/src/list/operation/nth.h @@ -1,8 +1,9 @@ #ifndef TYPEASVALUE_SRC_LIST_OPERATION_NTH_H_ #define TYPEASVALUE_SRC_LIST_OPERATION_NTH_H_ -#include "operation/math.h" #include "drop.h" +#include "conditional/if.h" +#include "operation/math.h" namespace tav { -- cgit v1.2.3