On Mon, Nov 29, 2021 at 9:30 AM Sebastian Berg <sebastian@sipsolutions.net> wrote:
On Thu, 2021-11-25 at 18:16 -0600, Juan Nunez-Iglesias wrote:
I have to say I like this! Together with partial functions / toolz currying [1] it could make for some rather elegant code:
result = gaussian_filter(image)[greater_than(t)]
On first sight, I am not yet convinced since we only save one line of code. My main reason is that unlike pandas, NumPy never tried to make method-chaining convenient. Is this really very useful unless you also embrace method-chaining?
I think method-chaining is a misdirection. You run into this problem whenever you have a larger expression, whether it is made up of method calls or function calls or operators. I've run into this problem a lot, especially when working interactively. With that being said, personally I think assignment expressions solve this problem just fine, and are no more unreadable than a lambda. Aaron Meurer
`arr.filter()` could also work, but likely we won't be able to easily agree on adding another method. And if you are already currying, maybe a small helper is almost as good?
Cheers,
Sebastian
Juan.
..[1]: https://toolz.readthedocs.io/en/latest/curry.html
On Wed, 24 Nov 2021, at 9:37 AM, cameron.pinnegar@gmail.com wrote:
If you have an array built up out of method chaining, sometimes you need to filter it at the very end. This can be annoying because it means you have to create a temporary variable just so you can refer to it in the indexing square brackets:
_temp = long_and_complicated_expression() result = _temp[_temp >= 0]
You could also use the walrus operator but this is odd looking and it still pollutes the namespace:
result = (_temp := long_and_complicated_expression())[_temp >= 0]
What I would like is to be able to use a lambda inside the indexing square brackets, which would take the whole array as an argument and give a boolean array:
result = long_and_complicated_expression()[lambda arr: arr >= 0]
I should emphasize, the lambda gets the entire array as its argument, and returns an entire mask array of bools. It isn't like the `map` and `filter` builtins where it would call the python function once for each element and thus be slow.
Pandas already has something similar[1]; you can pass a lambda into `.loc[]` that takes a Series and returns a boolean indexer.
[1] https://pandas.pydata.org/pandas-docs/version/0.18.1/whatsnew.html#method-ch... _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: jni@fastmail.com
NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: sebastian@sipsolutions.net
_______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: asmeurer@gmail.com