-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Idxmin & idxmax #12993
Idxmin & idxmax #12993
Conversation
this doesnt work correctly. let a: array[-1..1, int] = [0, 1, 2]
echo a[a.idxmin] == 0 this fails to compile type Count = enum First, Second, Third
let a: array[Count, int] = [0, 1, 2]
echo a[a.idxmin] == 0 |
|
Isn't this equivalent to just doing |
It is however |
Thank you |
The reason why your first example doesn't work is because the indices of OpenArrays go from 0 to N-1. Perhaps it would be better not to use OpenArrays for this proc but I will have to look into it. Regarding the second example I'm not sure how to easily fix it at the moment. |
@imperator26 |
What is this related to? |
@Imperator26 Sorry, it was a dumb suggestion, I guess you need seperate procs for array and seq to fix the issues this approach has |
The return type must be var m = idxmin(myarray) # returns 0; m is inferred to be Natural
dec m # dies at runtime And yes, this hints at a general issue with |
The loops should start at 0! Ping @Imperator26 |
If it's not working on your system it's because you've set
I think the result should be Natural since it's an index of an array and negative index are not supported by OpenArrays. If you want to do |
Sorry, misread the code. Starting with 1 is correct. The return type must still be
That's all true but |
result = low(a)
for i in low(a)+1..high(a): is more correct |
|
Try this: type
MyArr = array[-1..1, int]
var arr: MyArr
arr[-1] = -1
echo idxmin(arr) Can this be renamed to |
Could you please read the code and the entire discussion? I'm getting tired of answering these kind of comments |
That is very constructive... Please read this comment |
Saying you are tired when discussing is also very constructive I guess, |
It does work keeping in mind the limitations of OpenArrays. You're welcome to contribute BTW |
My conclusion is this: manual says openarrays always start at 0, this function returns wrong result at some cases, please don't add this to stdlib. Have a nice one and bye |
If you read the whole discussion you would realise that those cases are still to be implemented, but I guess reading is an option for you. Please don't comment here again unless you've got some constructive suggestions to give us. |
@Imperator26 i kinda agree with your technical arguments, but your defense does sound somehwhat condescending, everybody is free to comment here. |
Ok, really sorry if I sounded too condemning of including this function but I don't think it is something that can be fixed (judging by the fact that open array are just pointer+len pairs. |
What I was trying to say is that one should read before asking the same thing twice |
ok, but they are valid unresolved questions? let's solve them: |
sorry, the second one doesnt make sense, but then it should be possible to just overload for |
@alehander92 thanks for rescuing the discussion you are really my hero <3 |
@Imperator26 as you can see my friend the nim community is the friendliest on github so stick around and we'll build a lot of cool software using nim. |
sorry, i was just trying to make sense of it, i hope @Imperator26 doesn't mind proc idxmin*[T, U](s: array[T, U]): T =
for i in low(s)..high(s):
if s[i] < s[result]: result = i seems to work for those examples to me and being consistent with @Imperator26 's logic to return valid index types (and it should be possible to separately overload e.g. range cases to always return int, but i do hit some surprises in generics) |
e.g. this works proc idxmin*[T; U](s: array[T, U]): auto =
var res = low(s)
for i in low(s).succ..high(s):
if s[i] < s[res]:
res = i
when T isnot enum: # we can use a concept checking for .int here ..
result = res.int
else:
result = res if Araq wants EDIT: remove |
( @Imperator26 notice you can use |
Thank you for this I'll have a closer look later |
The |
but why, if we can just support it with an additional overload: we need some kind of policy for those |
It's high mental overhead writing and reading the code that can deal with "-11 based indexing". |
this works now and I'm considering the name change |
Any thoughts on the names of the procedures? |
I like |
Yes I also think they're clearer names |
Narimiran took over, thanks for your work! |
The merged code does not address the use cases described here while the code in this pr does. Is there any reason to why the accepted code was redacted? |
|
I'm not sure about the first point but I can understand the reasons in the second one. Is there any chance for snippets like these ones to be gathered in an external library? |
Sure, feel free to create Nimble packages. |
These are two procedures that return the index of the corresponding minimum and maximum value.
A cleaner version of #12976