-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
filter with index in the parser does not filter #3260
Comments
That is a nice one. The tricky thing is that the index of the map and filter functions of mathjs is not a number but an array with one or multiple numbers, depending on the number of dimensions of the matrix. So the right way to do this is: filter([10, 20], f(x, index) = index[1] == 1) # [10] as expected
filter([10, 20], f(x, index) = index[1] == 1000) # [] as expected The JavaScript code accidentally gives the right results because |
Thank you Jos! My bad. So I missed that the index is an array of numbers, not a number and the type conversion that happens with Something curious is that Returning to the cases I supplied I think it went like this: filter([10, 20], f(x, index) = index == 1) # yields [10, 20]
# [1] == 1 yields [true] which is thruthy
# [2] == 1 yields [false] which is thruthy
filter([10, 20], f(x, index) = index == 1000) # yields [10, 20]
# [1] == 1000 yields [false] which is thruthy
# [2] == 1000 yields [false] which is thruthy Now I understand it's not an issue of mathjs as the convention is for the index to be an array of numbers because in most contexts it can take n dimensional arrays. Understanding this is not an issue, I don't know if for convenience, it would be nice for contexts that are 1D arrays (like |
That is a good point, I've added this to the list with breaking changes for v14 as a reminder. |
I gave it some more thought. What about an automatic conversion, if the index is a number, convert it to an array with only that number. Similar to what js already does where Would that be a breaking change? |
So then:
It is an interesting idea. I'm not sure if it is a good idea though, it is kind of an inconsistent behavior. This requires the callback to have knowledge about the number of dimensions of the array that it is operating on 🤔. Maybe it would be better to create a different version of |
Hi Jos, My intention was to make a proposal for filter to automatically convert a value that is not an array to an array containing that value automatically. I did some attempts to use the original callback but couldn't make it work and then got occupied with other stuff. Just to let you know it's on the back of my mind but I don't think I will be able to make a proof of concept until December. |
Thanks for the update. Before we put effort in a PR or POC I think we should decide on whether we want to adjust this behavior or not. I'll give that some more thought soon. |
Ok, thanks, I'm not sure if it was possible, so I was checking if there was an easy way to do both. As a summary the idea was for both methods to work. X = [10, 20, 30];
filter(X, f(x, i) = x + i[1]> 11) # as index is always an array of one number it should always be i[1]
# Yields [20, 30]
filter(X, f(x, i) = x + i > 11)
# Currently yields [10, 20, 30] but the idea is to yield [20, 30] In a similar fashion I understand there are more implications regarding the integrity of the language. I'll postpone any POC until we have a clearer view of the desired behavior. |
I understand your idea, though I don't have an idea on how to make that work 🤔. |
I've been thinking about pros and cons of changing Pros:
Cons:
I think the downsides are bigger than the upsides, I value consistency and non-breaking changes quite highly. Also, when using the I think we should at least improve the documentation of |
Hi Jos, thanks for the through consideration and review. I don't have further comments in favor of this behavior other than what was previously discussed. Specially since I was unable to make it work for both cases I tend to agree with your decision and I have no strong objections about keeping it what it is, since I seldom use filter with an index. I agree further documentation of index is needed for the callbacks in As a side note, I noticed there is a function called |
I've improved the documentation and type definitions via 67ddc72. This is no breaking change. |
The improvements are published now in |
Hi,
Just found out this edge case in the parser on v13.1.1
Outside the parser there is no issue.
I found it while working on #3256, I'm still trying to find the source of the error.
The text was updated successfully, but these errors were encountered: