38#include <gtest/gtest.h>
119 if (x > 3)
return false;
195 auto squared = v.
maps<
int>([](
int x) {
return x * x; });
212 string expected[] = {
"1",
"2",
"3"};
221 auto evens = v.
filter([](
int x) {
return x % 2 == 0; });
234 auto evens = v.
filter([](
int x) {
return x % 2 == 0; });
242 int sum = v.
foldl<
int>(0, [](
int acc,
int x) {
return acc + x; });
253 int result = v.
foldl<
int>(42, [](
int acc,
int x) {
return acc + x; });
324 auto empty = v.
drop(100);
366 int* p = v.
find_ptr([](
int x) {
return x == 3; });
378 int* p = v.
find_ptr([](
int x) {
return x == 3; });
414 auto items = v.
items();
428 auto evens = v.
filter([](
int x) {
return x % 2 == 0; });
443 auto squared = v.
maps<
int>([](
int x) {
return x * x; });
507 EXPECT_EQ(v.
sum(
string(
"Prefix: ")),
"Prefix: Hello World");
572 const string*
shortest = v.
min_by([](
const string& a,
const string& b) {
573 return a.length() < b.length();
584 const string*
longest = v.
max_by([](
const string& a,
const string& b) {
585 return a.length() < b.length();
650 auto is_odd = [](
int x) {
return x % 2 != 0; };
679 const int* ptr = v.
first();
703 const int* ptr = v.
last();
804 auto evens = v.
filter([](
int x) {
return x % 2 == 0; });
830 return acc + (p.first % 2 == 0 ? 1 : 0);
842 constexpr size_t N = 10000;
845 for (
size_t i = 0; i <
N; ++i)
849 size_t sum = v.
foldl<
size_t>(0, [](
size_t acc,
size_t x) {
return acc + x; });
856 auto evens = v.
filter([](
size_t x) {
return x % 2 == 0; });
880 constexpr size_t N = 1000;
883 for (
size_t i = 0; i <
N; ++i)
884 v.
append(
static_cast<int>(i * 2));
893 EXPECT_EQ(p.second,
static_cast<int>(idx * 2));
1036 auto u = v.
unique_by([](
const string& a,
const string& b) {
1235 string s = v.
join(
string(
", "));
1242 string s = v.
join(
string(
"-"));
1249 string s = v.
join(
string(
", "));
1358 for (
int i = 1; i <= 5; ++i)
1362 int sum = list.
foldl<
int>(0, [](
int acc,
int x) {
return acc + x; });
1366 auto evens = list.
filter([](
int x) {
return x % 2 == 0; });
CRTP Mixins for container functionality (DRY principle).
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Append a new item by copy.
void empty() noexcept
empty the list
CRTP Mixin providing functional programming operations.
size_t find_index(Predicate pred) const
Find the index of the first element satisfying a predicate.
bool all(Operation &operation) const
Test if all elements satisfy a predicate.
const Type * min_by(Compare cmp) const
Find the minimum element using a custom comparator.
auto mutable_for_each(Operation &operation) -> decltype(self())
Apply an operation to each element (mutable).
Container< Type > drop(const size_t n) const
Skip the first n elements.
Container< std::pair< size_t, Type > > enumerate() const
Enumerate elements with their indices.
Container< Container< Type > > chunk(size_t n) const
Split into chunks of fixed size.
size_t count_if(Predicate pred) const
Count elements satisfying a predicate.
DynListType to_dynlist() const
Convert container to DynList.
DynList< Type > filter(Operation &operation) const
Filter elements by a predicate.
const Type * max() const
Find the maximum element.
Container< Type > rev() const
Create a reversed copy.
Container< __Type > maps(Operation &operation) const
Transform elements using a mapping function.
Container< Container< Type > > sliding(size_t size, size_t step=1) const
Create sliding windows of fixed size.
Container< Type > take(const size_t n) const
Take the first n elements.
std::string join_str(const std::string &sep=", ") const
Join string elements with separator.
Container< Type > unique() const
Remove consecutive duplicate elements.
const Type * first() const
Get the first element.
Container< std::pair< Type, typename Other::Item_Type > > zip_with(const Other &other) const
Zip with another container.
bool none(Predicate &pred) const
Check if no element satisfies a predicate.
Type last_or(const Type &default_val) const
Get the last element or a default value.
bool has_value(const Type &val) const
Check if container has a value.
StringType join(const StringType &sep=StringType{", "}) const
Join elements into a string with separator.
bool exists(Operation &operation) const
Test if any element satisfies a predicate.
size_t index_of(const Type &val) const
Find the index of a specific value.
__Type foldl(const __Type &init, std::function< __Type(const __Type &, const Type &)> operation) const
Left fold (reduce) with initial value.
auto for_each(Operation &operation) const -> decltype(self())
Apply an operation to each element (read-only).
Type product(const Type &init) const
Compute the product of all elements.
size_t length() const noexcept
Count the number of elements.
Container< Type > unique_by(EqPred eq) const
Remove consecutive duplicates using a custom equality predicate.
Type sum(const Type &init=Type{}) const
Compute the sum of all elements.
const Type * max_by(Compare cmp) const
Find the maximum element using a custom comparator.
std::pair< DynList< Type >, DynList< Type > > partition(Operation &op) const
Partition elements by a predicate.
std::vector< Type > to_vector() const
Convert to std::vector.
Container< Type > intersperse(const Type &sep) const
Intersperse a separator between elements.
Type first_or(const Type &default_val) const
Get the first element or a default value.
const Type * last() const
Get the last element.
const Type * min() const
Find the minimum element.
constexpr bool is_empty() const noexcept
Return true if list is empty.
size_t size() const noexcept
Count the number of elements of the list.
CRTP Mixin for extracting keys from set-like containers.
Container< Type > items() const
Alias for keys().
Container< Type > keys() const
Extract all keys as a list.
CRTP Mixin providing element location operations.
std::tuple< bool, Type > find_item(Operation &operation)
Find element with success flag.
Type * find_ptr(Operation &operation)
Find the first element satisfying a predicate.
Type & nth_ne(const size_t n) const noexcept
Access the n-th element (unchecked).
Type & nth(const size_t n) const
Access the n-th element (bounds-checked).
CRTP Mixin providing traversal operations.
bool traverse(Operation &operation) const
Traverse all elements, applying an operation to each.
__T foldl(const __T &init, Op &op) const
Fold the elements of the container to a specific result.
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criteria.
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criteria.
bool all(Operation &operation) const
Check if all the elements of container satisfy a condition.
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Type * find_ptr(Operation &operation) noexcept(operation_is_noexcept< Operation >())
Find a pointer to an item in the container according to a searching criteria.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Iterator(const MixinVector &c)
const MixinVector * container
bool has_curr() const noexcept
Simple test container using std::vector internally but with Aleph mixins.
void insert(const T &item)
bool operator==(const MixinVector &other) const
void append(const T &item)
MixinVector(std::initializer_list< T > init)
size_t size() const noexcept
bool is_empty() const noexcept
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
std::string tolower(const char *str)
Convert a C std::string to lower-case.
const Container::Item_Type * min_ptr(const Container &container, Cmp cmp=Cmp())
Find the minimum element in a container.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
bool completed() const noexcept
Return true if all underlying iterators are finished.
std::decay_t< typename HeadC::Item_Type > T
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
const Container::Item_Type * max_ptr(const Container &container, Cmp cmp=Cmp())
Find the maximum element in a container.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
DynList< T > maps(const C &c, Op op)
Classic map operation.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Represents a missing value.
bool operator==(const EqOnlyType &other) const
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation >())
Traverse the container via its iterator and performs a conditioned operation on each item.
bool operator<(const LtOnlyType &other) const