Custom Path Function SelectEvenIndexes

The SelectEvenIndexes path function selects elements at even-numbered indexes (0, 2, 4, etc.) from a collection, with optional predicate filtering.

Description

This path function iterates through a collection, selecting only elements at even indexes (starting from 0). It can optionally apply a predicate filter to further refine the selection. The function processes indexes 0, 2, 4, 6, and so on, skipping odd-indexed elements.

Syntax

collection.SelectEvenIndexes(predicate)

Parameters

Parameter

Type

Required

Description

predicate

Lambda

Yes

A lambda expression that takes a single parameter representing the current element and returns a boolean. Only elements at even indexes that satisfy this predicate are included in the result. Format: x => condition

Return Value

  • Type: Collection

  • Returns: A filtered collection containing only elements at even indexes that satisfy the optional predicate condition.

Implementation Details

The SelectEvenIndexes function is implemented through the SelectEvenIndexesCollectionItemsPathElement class, which:

  • Iterates through the collection with a step of 2 (indexes 0, 2, 4, …)

  • Applies the predicate filter to each even-indexed element

  • Registers and manages lambda parameter variables

  • Returns a new collection with the selected elements

Examples:

{
  "Example1": "$value([1, 2, 3, 4, 5, 6].SelectEvenIndexes(x => true))",
  // Result: [1, 3, 5] (elements at indexes 0, 2, 4)

  "Example2": "$value([10, 20, 30, 40, 50].SelectEvenIndexes(x => x > 15))",
  // Result: [30, 50] (elements at even indexes where value > 15)

  "Example3": "$value(Employees.SelectEvenIndexes(e => e.Salary > 90000))",
  // Selects employees at even indexes with salary greater than 90000

  "Example4": "$value(Companies.Select(c => c.Employees).Flatten().SelectEvenIndexes(e => e.Id != 100000001))",
  // Flattens all employees, selects even indexes, excludes specific ID

  "Example5": "$value(TestData.SelectEvenIndexes(x => x != null))",
  // Selects non-null elements at even indexes from TestData array
}

Use Cases

The SelectEvenIndexes function is useful for:

  • Sampling Data: Selecting every other element for statistical sampling

  • Pattern-Based Selection: Extracting elements following a specific pattern

  • Data Reduction: Reducing dataset size while maintaining distribution

  • Alternating Processing: Processing alternate items in a sequence

  • Custom Filtering Logic: Combining index-based and predicate-based filtering

Note

This is a demonstration custom path function. The index-based selection (even indexes) combined with predicate filtering provides a unique pattern for element selection that differs from standard filtering operations.