-
Notifications
You must be signed in to change notification settings - Fork 1
Implement filter builtin function
#63
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
base: main
Are you sure you want to change the base?
Conversation
filter builtin function
crates/monty/src/builtins/filter.rs
Outdated
| let (mut positional, kwargs) = args.into_parts(); | ||
|
|
||
| if !kwargs.is_empty() { | ||
| for (k, v) in kwargs { | ||
| k.drop_with_heap(heap); | ||
| v.drop_with_heap(heap); | ||
| } | ||
|
|
||
| for v in positional { | ||
| v.drop_with_heap(heap); | ||
| } | ||
|
|
||
| return Err(SimpleException::new_msg(ExcType::TypeError, "filter() does not support keyword arguments").into()); | ||
| } | ||
|
|
||
| // Check we have exactly 2 positional arguments | ||
| let positional_len = positional.len(); | ||
| if positional_len != 2 { | ||
| for v in positional { | ||
| v.drop_with_heap(heap); | ||
| } | ||
| return Err(SimpleException::new_msg( | ||
| ExcType::TypeError, | ||
| format!("filter() expected 2 arguments, got {positional_len}"), | ||
| ) | ||
| .into()); | ||
| } | ||
|
|
||
| let function = positional.next().expect("positional must have 2 elements"); | ||
| let iterable = positional.next().expect("positional must have 2 elements"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can use get_one_two_args for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very nice, we can probably update a bunch of other builtin functions like sorted that manually parse args atm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. Claude can go though and fix them all I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we also need tests for a non-callable discriminator, like filter(4, [1, 2])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See crates/monty/test_cases/builtin__filter_not_callable.py
e97204a to
fe37fa4
Compare
fe37fa4 to
7629de6
Compare
samuelcolvin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, but I forgot we also need support for use defined functions which might require a bit of logic change, look at list.sort for an example.
This PR implements the
filter()builtin function that filters elements from an iterable based on a predicateNote: the implementation returns a list (following the pattern of other iterator-returning builtins like enumerate() and zip() for simplicyt). Currently, only builtin functions are supported as predicates which matches the existing limitation in
sorted()with thekeyparam