Commit d2dff508 authored by Anton Bukov's avatar Anton Bukov

Restore std::function for filters and predicates

parent 350ff18d
...@@ -69,8 +69,7 @@ namespace boolinq { ...@@ -69,8 +69,7 @@ namespace boolinq {
return for_each_i([apply](T value, int index) { return apply(value); }); return for_each_i([apply](T value, int index) { return apply(value); });
} }
template<typename F> Linq<LinqIndex<S, T>, T> where_i(std::function<bool(T, int)> filter) const
Linq<LinqIndex<S, T>, T> where_i(F filter) const
{ {
return Linq<LinqIndex<S, T>, T>( return Linq<LinqIndex<S, T>, T>(
{*this, 0}, {*this, 0},
...@@ -85,8 +84,7 @@ namespace boolinq { ...@@ -85,8 +84,7 @@ namespace boolinq {
); );
} }
template<typename F> Linq<LinqIndex<S, T>, T> where(std::function<bool(T)> filter) const
Linq<LinqIndex<S, T>, T> where(F filter) const
{ {
return where_i([filter](T value, int index) { return filter(value); }); return where_i([filter](T value, int index) { return filter(value); });
} }
...@@ -101,8 +99,7 @@ namespace boolinq { ...@@ -101,8 +99,7 @@ namespace boolinq {
}); });
} }
template<typename F> Linq<LinqIndex<S, T>, T> takeWhile_i(std::function<bool(T, int)> predicate) const
Linq<LinqIndex<S, T>, T> takeWhile_i(F predicate) const
{ {
return where_i([predicate](T value, int i) { return where_i([predicate](T value, int i) {
if (!predicate(value, i)) { if (!predicate(value, i)) {
...@@ -112,8 +109,7 @@ namespace boolinq { ...@@ -112,8 +109,7 @@ namespace boolinq {
}); });
} }
template<typename F> Linq<LinqIndex<S, T>, T> takeWhile(std::function<bool(T)> predicate) const
Linq<LinqIndex<S, T>, T> takeWhile(F predicate) const
{ {
return takeWhile_i([predicate](T value, int /*i*/) { return predicate(value); }); return takeWhile_i([predicate](T value, int /*i*/) { return predicate(value); });
} }
...@@ -130,8 +126,7 @@ namespace boolinq { ...@@ -130,8 +126,7 @@ namespace boolinq {
bool flag; bool flag;
}; };
template<typename F> Linq<LinqIndexFlag<S, T>, T> skipWhile_i(std::function<bool(T, int)> predicate) const
Linq<LinqIndexFlag<S, T>, T> skipWhile_i(F predicate) const
{ {
return Linq<LinqIndexFlag<S, T>, T>( return Linq<LinqIndexFlag<S, T>, T>(
{*this, 0, false}, {*this, 0, false},
...@@ -150,8 +145,7 @@ namespace boolinq { ...@@ -150,8 +145,7 @@ namespace boolinq {
); );
} }
template<typename F> Linq<LinqIndexFlag<S, T>, T> skipWhile(std::function<bool(T)> predicate) const
Linq<LinqIndexFlag<S, T>, T> skipWhile(F predicate) const
{ {
return skipWhile_i([predicate](T value, int /*i*/) { return predicate(value); }); return skipWhile_i([predicate](T value, int /*i*/) { return predicate(value); });
} }
...@@ -213,7 +207,7 @@ namespace boolinq { ...@@ -213,7 +207,7 @@ namespace boolinq {
template< template<
typename F, typename F,
typename _TRet = typename std::result_of<F(T,int)>::type, typename _TRet = typename std::result_of<F(T, int)>::type,
typename _TRetVal = typename _TRet::value_type typename _TRetVal = typename _TRet::value_type
> >
Linq<LinqCurrentIndexFinished<S, T, _TRet>, _TRetVal> selectMany_i(F apply) const Linq<LinqCurrentIndexFinished<S, T, _TRet>, _TRetVal> selectMany_i(F apply) const
...@@ -416,8 +410,7 @@ namespace boolinq { ...@@ -416,8 +410,7 @@ namespace boolinq {
return index; return index;
} }
template<typename F> int count(std::function<bool(T)> predicate) const
int count(F predicate) const
{ {
return where(predicate).count(); return where(predicate).count();
} }
...@@ -429,8 +422,7 @@ namespace boolinq { ...@@ -429,8 +422,7 @@ namespace boolinq {
// Bool aggregators // Bool aggregators
template<typename F> bool any(std::function<bool(T)> predicate) const
bool any(F predicate) const
{ {
Linq<S, T> linq = *this; Linq<S, T> linq = *this;
try { try {
...@@ -448,8 +440,7 @@ namespace boolinq { ...@@ -448,8 +440,7 @@ namespace boolinq {
return any([](T value) { return static_cast<bool>(value); }); return any([](T value) { return static_cast<bool>(value); });
} }
template<typename F> bool all(std::function<bool(T)> predicate) const
bool all(F predicate) const
{ {
return !any([predicate](T value) { return !predicate(value); }); return !any([predicate](T value) { return !predicate(value); });
} }
...@@ -513,8 +504,7 @@ namespace boolinq { ...@@ -513,8 +504,7 @@ namespace boolinq {
return skip(index).next(); return skip(index).next();
} }
template<typename F> T first(std::function<bool(T)> predicate) const
T first(F predicate) const
{ {
return where(predicate).next(); return where(predicate).next();
} }
...@@ -524,8 +514,7 @@ namespace boolinq { ...@@ -524,8 +514,7 @@ namespace boolinq {
return next(); return next();
} }
template<typename F> T firstOrDefault(std::function<bool(T)> predicate) const
T firstOrDefault(F predicate) const
{ {
try { try {
return where(predicate).next(); return where(predicate).next();
...@@ -539,8 +528,7 @@ namespace boolinq { ...@@ -539,8 +528,7 @@ namespace boolinq {
firstOrDefault([](T /*value*/) { return true; }); firstOrDefault([](T /*value*/) { return true; });
} }
template<typename F> T last(std::function<bool(T)> predicate) const
T last(F predicate) const
{ {
T res; T res;
for_each([&res](T value) { for_each([&res](T value) {
...@@ -554,8 +542,7 @@ namespace boolinq { ...@@ -554,8 +542,7 @@ namespace boolinq {
return last([](T /*value*/) { return true; }); return last([](T /*value*/) { return true; });
} }
template<typename F> T lastOrDefault(std::function<bool(T)> predicate) const
T lastOrDefault(F predicate) const
{ {
try { try {
return last(predicate); return last(predicate);
...@@ -771,7 +758,7 @@ namespace boolinq { ...@@ -771,7 +758,7 @@ namespace boolinq {
template<typename T, int N> template<typename T, int N>
Linq<std::pair<T *, T *>, T> from(T (&array)[N]) Linq<std::pair<T *, T *>, T> from(T (&array)[N])
{ {
return from((T *) (&array), (T *) (&array) + N); return from((T *)(&array), (T *)(&array) + N);
} }
template<template<class> class TV, typename TT> template<template<class> class TV, typename TT>
...@@ -782,32 +769,32 @@ namespace boolinq { ...@@ -782,32 +769,32 @@ namespace boolinq {
} }
// std::list, std::vector, std::dequeue // std::list, std::vector, std::dequeue
template<template<class,class> class TV, typename TT, typename TU> template<template<class, class> class TV, typename TT, typename TU>
auto from(const TV<TT,TU> & container) auto from(const TV<TT, TU> & container)
-> decltype(from(std::begin(container), std::end(container))) -> decltype(from(std::begin(container), std::end(container)))
{ {
return from(std::begin(container), std::end(container)); return from(std::begin(container), std::end(container));
} }
// std::set // std::set
template<template<class,class,class> class TV, typename TT, typename TS, typename TU> template<template<class, class, class> class TV, typename TT, typename TS, typename TU>
auto from(const TV<TT,TS,TU> & container) auto from(const TV<TT, TS, TU> & container)
-> decltype(from(std::begin(container), std::end(container))) -> decltype(from(std::begin(container), std::end(container)))
{ {
return from(std::begin(container), std::end(container)); return from(std::begin(container), std::end(container));
} }
// std::map // std::map
template<template<class,class,class,class> class TV, typename TK, typename TT, typename TS, typename TU> template<template<class, class, class, class> class TV, typename TK, typename TT, typename TS, typename TU>
auto from(const TV<TK,TT,TS,TU> & container) auto from(const TV<TK, TT, TS, TU> & container)
-> decltype(from(std::begin(container), std::end(container))) -> decltype(from(std::begin(container), std::end(container)))
{ {
return from(std::begin(container), std::end(container)); return from(std::begin(container), std::end(container));
} }
// std::array // std::array
template<template<class,size_t> class TV, typename TT, size_t TL> template<template<class, size_t> class TV, typename TT, size_t TL>
auto from(const TV<TT,TL> & container) auto from(const TV<TT, TL> & container)
-> decltype(from(std::begin(container), std::end(container))) -> decltype(from(std::begin(container), std::end(container)))
{ {
return from(std::begin(container), std::end(container)); return from(std::begin(container), std::end(container));
...@@ -828,10 +815,10 @@ namespace boolinq { ...@@ -828,10 +815,10 @@ namespace boolinq {
} }
template<typename T> template<typename T>
Linq<std::pair<std::pair<T,T>,T>,T> range(T start, T end, T step) { Linq<std::pair<std::pair<T, T>, T>, T> range(T start, T end, T step) {
return Linq<std::pair<std::pair<T,T>,T>,T>( return Linq<std::pair<std::pair<T, T>, T>, T>(
{{start, end}, step}, {{start, end}, step},
[](std::pair<std::pair<T,T>,T> &tuple) { [](std::pair<std::pair<T, T>, T> &tuple) {
if (tuple.first.first < tuple.first.second) { if (tuple.first.first < tuple.first.second) {
return *(tuple.first.first += tuple.second); return *(tuple.first.first += tuple.second);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment