Functional-style collection operations with composable query conditions
simple_container provides functional-style operations for Eiffel collections without cursor manipulation headaches. Instead of manually managing cursors with start, forth, and after, you use composable query conditions and declarative operations like filtered, take, drop, and partition.
The library introduces a boolean algebra of query conditions where you can combine predicates using and, or, and not operators.
local
l_list: ARRAYED_LIST [INTEGER]
l_query: SIMPLE_LIST_QUERY [INTEGER]
l_cond: SIMPLE_PREDICATE_CONDITION [INTEGER]
l_result: ARRAYED_LIST [INTEGER]
do
create l_list.make_from_array (<<1, 2, 3, 4, 5, 6, 7, 8, 9, 10>>)
create l_query.make (l_list)
create l_cond.make (agent (i: INTEGER): BOOLEAN do Result := i > 5 end)
l_result := l_query.filtered (l_cond)
-- l_result contains: 6, 7, 8, 9, 10
end
Abstract base for composable boolean conditions.
| Feature | Description |
|---|---|
satisfied_by (a_item) | Check if item satisfies condition |
conjuncted alias "and" | Combine with AND |
disjuncted alias "or" | Combine with OR |
negated alias "not" | Negate condition |
Cursor-safe query operations on lists.
| Feature | Description |
|---|---|
filtered (a_condition) | Items satisfying condition |
first_satisfying (a_condition) | First matching item or Void |
all_satisfy (a_condition) | Do all items satisfy? |
any_satisfies (a_condition) | Does any item satisfy? |
count_satisfying (a_condition) | Count of matching items |
mapped (a_function) | Apply function to each item |
folded (a_initial, a_combiner) | Reduce to single value |
Extension operations for lists.
| Feature | Description |
|---|---|
partition (a_condition) | Split into satisfying/not satisfying |
group_by (a_key_function) | Group by key |
min_by (a_selector) | Element with minimum selector value |
max_by (a_selector) | Element with maximum selector value |
index_of_first (a_condition) | Index of first matching element |
index_of_last (a_condition) | Index of last matching element |
reversed | Items in reverse order |
take (n) | First n items |
drop (n) | All except first n items |
zip (a_other) | Combine lists element-by-element |
chunked (a_size) | Split into fixed-size sublists |
windowed (a_size) | Sliding window sublists |
Sorting operations for lists.
| Feature | Description |
|---|---|
sorted_by (a_key) | Elements sorted by key (ascending) |
sorted_by_descending (a_key) | Elements sorted by key (descending) |
Distinct operations for hashable lists.
| Feature | Description |
|---|---|
distinct | Elements with duplicates removed |
distinct_by (a_key) | Elements with duplicates by key removed |
Lazy slice view into arrays/lists (no copying).
| Feature | Description |
|---|---|
make (a_source, a_start, a_end) | Create slice from range |
item alias "[]" (i) | Item at position |
to_array / to_list | Convert to concrete container |
sub_slice | Create sub-slice |
Set operations on collections.
| Feature | Description |
|---|---|
union | All items in either collection |
intersection | Items in both collections |
difference | Items in first but not second |
is_subset | Check if subset |
Add to your .ecf file:
<library name="simple_container" location="$SIMPLE_LIBS/simple_container/simple_container.ecf"/>