-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[stdlib] Add filter_types to variadic
#5714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saviorand
wants to merge
3
commits into
modular:main
Choose a base branch
from
saviorand:add-exclude-keep-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
e018854 to
5a6f21a
Compare
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
filter_types to variadic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A few convenience functions to make it easier to work with variadics