Skip to content
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

maxIndex but not minIndex sometimes returns the index as an array with a single element #10567

Open
dlangBugzillaToGithub opened this issue Nov 13, 2024 · 3 comments

Comments

@dlangBugzillaToGithub
Copy link

pabuond reported this on 2024-11-13T19:28:18Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=24858

CC List

  • kdevel
  • nick (@ntrel)
  • pabuond

Description

`maxIndex` returns an array with a single element when run on an array modified by UFCS (e.g. for absolute value). `minIndex` doesn't, so I guess this is a bug.
Tested with dmd v2.109.1

```
import std.math: fabs;
import std.algorithm: minIndex, maxIndex;

double[] u = [-9, 3, 2, 8, 4];
writeln(u.maxIndex); // => 3, OK
writeln(u.map!fabs.minIndex); // => 2, OK
writeln(u.map!fabs.maxIndex); // => [0] ??? Expected: 0
```
Thanks!
@dlangBugzillaToGithub
Copy link
Author

nick (@ntrel) commented on 2024-11-15T11:42:33Z

Are you sure? With 2.109.0 the last line does give `0`.

@dlangBugzillaToGithub
Copy link
Author

kdevel commented on 2024-11-15T12:48:44Z

WORKSFORME

compilable version (mi.d):

```
import std.stdio : writeln;
import std.algorithm : map;
import std.math: fabs;
import std.algorithm: minIndex, maxIndex;

unittest
{
   double[] u = [-9, 3, 2, 8, 4];
   writeln(u.maxIndex); // => 3, OK
   writeln(u.map!fabs.minIndex); // => 2, OK
   writeln(u.map!fabs.maxIndex); // => [0] ??? Expected: 0
}
```

$ dmd --version
DMD64 D Compiler v2.109.1
Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved written by Walter Bright
$ dmd -unittest -main -run mi.d 
3
2
0
1 modules passed unittests

(also works under dmd 2.098, GDC 12.1, GDC 11.3)

@dlangBugzillaToGithub
Copy link
Author

pabuond commented on 2024-11-15T13:27:18Z

Aw, my bad - I was doing some tests, but in combination with mir.ndslice and it turns out it's due to a clash with mir.ndslice's maxIndex/minIndex that I didn't realise I was importing too. So not a bug in std.algorithm :) sorry for the false alarm! Not too sure exactly what creates the issue, some import combinations work, but one doesn't. (Of course, it's bad to import homonyms into the namespace - the compiler seemed to deal with it, so I thought it was fine, but obviously it creates some issues. There doesn't seem to be a way to use UFCS with full module names, is there?)
```
import std.stdio: writeln;
import std.math: fabs;

/* OK */
// import std.algorithm: maxIndex, minIndex, map;

/* OK */
// import std.algorithm: maxIndex, minIndex;
// import mir.ndslice: map;

/* OK */
// import std.algorithm: maxIndex, minIndex, map;
// import mir.ndslice: maxIndex, minIndex;

/* Not OK */
import std.algorithm: maxIndex, minIndex;
import mir.ndslice: maxIndex, minIndex, map;

void main()
{
	double[] u = [-9, 3, 2, 8, 4];
	writeln(u.maxIndex); // => 3, OK
	writeln(u.minIndex); // => 0, OK
	writeln(u.map!fabs.minIndex); // => [2] ???
	writeln(u.map!fabs.maxIndex); // => [0] ???
}
```

@thewilsonator thewilsonator removed OS:Linux Issues specific to Linux Arch:x86_64 Issues specific to x86_64 P1 labels Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants