Skip to content

Conversation

@saviorand
Copy link

A few convenience functions to make it easier to work with variadics

@saviorand saviorand requested a review from a team as a code owner December 28, 2025 12:02
@github-actions
Copy link

github-actions bot commented Dec 28, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@saviorand saviorand changed the title Add exclude and keep types to variadic [stdlib] Add exclude and keep types to variadic Dec 28, 2025
@saviorand
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

modular-cla-bot bot added a commit to modular/cla that referenced this pull request Dec 28, 2025
@saviorand saviorand force-pushed the add-exclude-keep-types branch from e018854 to 5a6f21a Compare December 28, 2025 12:25
BEGIN_PUBLIC
[Stdlib] Add variadic type filtering utilities

Adds three new compile-time utilities to `Variadic` for filtering type sequences:

- `exclude_type[*types, type=T]` - Remove a single type from a variadic
- `keep_types[*types, keep_types=...]` - Keep only specified types
- `exclude_types[*types, exclude_types=...]` - Remove multiple types

These utilities enable ergonomic manipulation of `Variant` types and other
variadic type sequences at compile time.

Example usage:
```mojo
comptime Filtered = Variadic.exclude_type[*Variant[Int, String, Bool].Ts, type=Int]
comptime NewVariant = Variant[*Filtered]  // Variant[String, Bool]
```

Includes comprehensive tests demonstrating all three filtering modes and
chaining behavior.
END_PUBLIC
BEGIN_PUBLIC
[Stdlib] Improve variadic filtering API and add comprehensive tests

Updates the variadic filtering utilities to use variadic parameters for a more
ergonomic API, and adds comprehensive test coverage:

API improvements:
- Changed from `element_types=...` to `*element_types` variadic parameters
- Renamed `Trait` to `T` to match stdlib conventions (consistent with
  `Variadic.types`, `Variadic.slice_types`, etc.)
- Simplified parameter names: `type` instead of `exclude_type` for single exclusions

Usage is now more concise:
```mojo
// Before
Variadic.exclude_type[exclude_type=Int, element_types=MyTypes]

// After
Variadic.exclude_type[*MyTypes, type=Int]
```

Tests added:
- `test_exclude_type()` - Remove a single type from a variadic
- `test_keep_types()` - Keep only specified types
- `test_exclude_types()` - Remove multiple types
- `test_filtering_chained()` - Chain multiple filtering operations
END_PUBLIC
BEGIN_PUBLIC
[Stdlib] Replace specialized filters with generic filter_types

Replaces the three specialized filtering functions (`exclude_type`, `keep_types`,
`exclude_types`) with a single generic `filter_types` function that accepts a
user-defined predicate.

Implementation:
- Added `_TypePredicateGenerator[T]` - Generator type for predicates that take
  a type and return Bool
- Added `_FilterReducer` - Generic reducer that filters based on any predicate
- Added `Variadic.filter_types[*types, predicate=...]` - Public API

This design is more flexible and composable than having separate specialized
functions. Users can write any filtering logic they need:

```mojo
# Exclude a single type
comptime IsNotInt[Type: Movable] = not _type_is_eq[Type, Int]()
comptime Filtered = Variadic.filter_types[*MyTypes, predicate=IsNotInt]

# Keep only specific types
comptime IsNumeric[Type: Movable] = (
    _type_is_eq[Type, Int]() or _type_is_eq[Type, Float64]()
)
comptime Numeric = Variadic.filter_types[*MyTypes, predicate=IsNumeric]

# Exclude multiple types
comptime NotIntOrBool[Type: Movable] = (
    not _type_is_eq[Type, Int]() and not _type_is_eq[Type, Bool]()
)
comptime Filtered = Variadic.filter_types[*MyTypes, predicate=NotIntOrBool]
```

Tests include filtering to exclude single types, keep only specific types,
exclude multiple types, and chaining filter operations.
END_PUBLIC
@saviorand saviorand changed the title [stdlib] Add exclude and keep types to variadic [stdlib] Add filter_types to variadic Dec 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants