From 27b991ea23f71093b274b3ff1692c47274eb4d1d Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Mon, 26 Jan 2015 19:19:45 +0100 Subject: Redefined `Length` in terms of `Apply` and `Fold` * this function illustrates the use case `Apply` is currently intended for * moved `Take` and `Nth` into separate files to resolve header resolution conflicts --- src/list/list.h | 2 ++ src/list/operation/basic.h | 72 +++++----------------------------------------- src/list/operation/nth.h | 31 ++++++++++++++++++++ src/list/operation/take.h | 40 ++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 65 deletions(-) create mode 100644 src/list/operation/nth.h create mode 100644 src/list/operation/take.h (limited to 'src/list') diff --git a/src/list/list.h b/src/list/list.h index 5c67872..c03a0ef 100644 --- a/src/list/list.h +++ b/src/list/list.h @@ -30,6 +30,8 @@ using Tail = Cdr; } #include "operation/basic.h" +#include "operation/nth.h" +#include "operation/take.h" #include "operation/append.h" #include "operation/concatenate.h" diff --git a/src/list/operation/basic.h b/src/list/operation/basic.h index d80b347..1f4b043 100644 --- a/src/list/operation/basic.h +++ b/src/list/operation/basic.h @@ -1,76 +1,18 @@ #ifndef TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_ #define TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_ -#include "operation/math.h" #include "higher/fold.h" +#include "function/apply.h" +#include "operation/math.h" namespace tav { template -class Length { - private: - template < - typename, - typename Previous - > - struct count { - typedef Add, Previous> type; - }; - - public: - typedef typename Fold, Cons>::type type; - -}; - -template < - typename Index, - typename Cons -> -struct Nth { - typedef typename Nth< - Substract>, - Tail - >::type type; -}; - -template -struct Nth { - typedef void type; -}; - -template -struct Nth, Cons> { - typedef Head type; -}; - -template < - typename Count, - typename Current -> -struct Take { - typedef Cons< - Head, - typename Take< - Substract>, - Tail - >::type - > type; -}; - -template -struct Take, Current> { - typedef void type; -}; - -template -struct Take { - typedef void type; -}; - -template <> -struct Take, void> { - typedef void type; -}; +using Length = Fold< + Apply, _1>::pair_type, + Size<0>, + Cons +>; } diff --git a/src/list/operation/nth.h b/src/list/operation/nth.h new file mode 100644 index 0000000..8eb88ba --- /dev/null +++ b/src/list/operation/nth.h @@ -0,0 +1,31 @@ +#ifndef TYPEASVALUE_SRC_LIST_OPERATION_NTH_H_ +#define TYPEASVALUE_SRC_LIST_OPERATION_NTH_H_ + +#include "operation/math.h" + +namespace tav { + +template < + typename Index, + typename Cons +> +struct Nth { + typedef typename Nth< + Substract>, + Tail + >::type type; +}; + +template +struct Nth, Cons> { + typedef Head type; +}; + +template +struct Nth { + typedef void type; +}; + +} + +#endif // TYPEASVALUE_SRC_LIST_OPERATION_NTH_H_ diff --git a/src/list/operation/take.h b/src/list/operation/take.h new file mode 100644 index 0000000..db25640 --- /dev/null +++ b/src/list/operation/take.h @@ -0,0 +1,40 @@ +#ifndef TYPEASVALUE_SRC_LIST_OPERATION_TAKE_H_ +#define TYPEASVALUE_SRC_LIST_OPERATION_TAKE_H_ + +#include "operation/math.h" + +namespace tav { + +template < + typename Count, + typename Current +> +struct Take { + typedef Cons< + Head, + typename Take< + Substract>, + Tail + >::type + > type; +}; + +template +struct Take, Current> { + typedef void type; +}; + +template +struct Take { + typedef void type; +}; + +template <> +struct Take, void> { + typedef void type; +}; + + +} + +#endif // TYPEASVALUE_SRC_LIST_OPERATION_TAKE_H_ -- cgit v1.2.3