[Issue 8573] New: A simpler Phobos function that returns the index of the mix or max item
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Aug 21 05:05:12 PDT 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8573
Summary: A simpler Phobos function that returns the index of
the mix or max item
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2012-08-21 05:05:07 PDT ---
countUntil returns a signed value with the position, or -1 if not found:
import std.algorithm: countUntil;
void main() {
assert(countUntil("hello world", 'r') == 8);
}
But to find the index of the minimum item things become more complex and more
bug-prone:
import std.algorithm: minPos;
import std.range: walkLength;
void main() {
immutable s = "hello world";
immutable sizediff_t mpos = s.walkLength() - minPos(s).walkLength();
assert(mpos == 5);
}
I have typed mpos "sizediff_t" to avoid troubles caused by a unsigned position
value.
In Python taking the sum() of an empty list (array) returns 0, while searching
for min/max in an empty list causes a ValueError exception to be thrown:
>>> a = []
>>> sum(a)
0
>>> min(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: min() arg is an empty sequence
In D here mpos is 0:
import std.algorithm: minPos;
import std.range: walkLength;
void main() {
immutable s = "";
immutable sizediff_t mpos = s.walkLength() - minPos(s).walkLength();
assert(mpos == 0);
}
So in Phobos I'd like a simple to use function that returns the sizediff_t
index of the min item (and when the input is empty, it returns -1 or throws an
exception).
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list