Bodu.Collections.Extensions Namespace
- Package
-
Bodu.Core 1.0.1
Purpose
Bodu.Collections.Extensions holds the non-generic / shape-agnostic enumeration helpers that complement Bodu.Collections.Generic.Extensions. Reach for this namespace when you want recursive-descent helpers or count helpers that work against IEnumerable rather than IEnumerable<T>.
Key types
- IEnumerableExtensions -
CountOrDefault,RecursiveSelectfor non-generic shapes. - RecursiveSelectControl -
[Flags]value returned by a recursion-control callback to decide, per element, whether to yield it, descend into its children, and whether to stop: the primitive flagsNone,Yield,Recurse,Skip,Break,Exit, and the named combinationsYieldOnly,RecurseOnly,YieldAndRecurse,SkipOnly,SkipAndRecurse,YieldAndBreak,SkipAndBreak,YieldAndExit,SkipAndExit. Shared with the typedRecursiveSelectoverloads in Bodu.Collections.Generic.Extensions.
Example
using Bodu.Collections.Extensions;
// Counts a non-generic IEnumerable - O(1) when the source is an ICollection, otherwise it enumerates.
int n = source.CountOrDefault();
// Recursive-select over a non-generic IEnumerable: the child selector and the result are untyped.
IEnumerable flattened = root.RecursiveSelect(node => ((Node)node).Children);
// Fine-grained descent control on the non-generic surface: yield visible nodes and descend into
// them, skip invisible subtrees, and stop the whole walk at the first node flagged as terminal.
IEnumerable visible = root.RecursiveSelect(
node => ((Node)node).Children,
(node, index, depth) => node,
node => ((Node)node).IsTerminal ? RecursiveSelectControl.YieldAndExit
: ((Node)node).IsVisible ? RecursiveSelectControl.YieldAndRecurse
: RecursiveSelectControl.SkipOnly);
When the source is already an IEnumerable<T>, prefer the typed overloads in Bodu.Collections.Generic.Extensions - the same RecursiveSelectControl vocabulary with strongly typed selectors and an IEnumerable<TResult> result.
Notes
- Counterpart to the generic surface. Sequence helpers parameterised on
Tlive in Bodu.Collections.Generic.Extensions; this namespace covers the cases where the source is shape-agnostic. - Packaging. This extension namespace ships in the
Bodu.Corepackage; the concrete collection types in the siblingBodu.Collections.*namespaces ship in theBodu.Collectionspackage, except the thread-safeBodu.Collections.Generic.Concurrentvariants, which ship inBodu.Collections.Concurrent. - See also: the Bodu.Core introduction, the companion Bodu.Collections.Generic.Extensions namespace.
Classes
- IEnumerableExtensions
Provides operations over the non-generic IEnumerable surface - counting without enumerating where the source is already a collection, and walking heterogeneous tree structures recursively - for code that has to deal with reflection-shaped or legacy sequences.
Enums
- RecursiveSelectControl
Represents composable control flags for how an element is processed during recursive selection.