[Issue 4705] Redesign of std.algorithm.max()/min() + mins()/maxs()
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Jan 1 06:35:14 PST 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4705
--- Comment #5 from bearophile_hugs at eml.cc 2011-01-01 06:33:02 PST ---
Another example to show why a better max() is useful. The task is ot show how
the number less than 1000 which has the longest hailstone sequence. For the
hailstone see:
http://en.wikipedia.org/wiki/Collatz_conjecture
The code, DMD 2.051:
import std.stdio, std.algorithm, std.range, std.typecons;
auto hailstone(int n) {
auto result = [n];
while (n != 1) {
n = n & 1 ? n*3 + 1 : n/2;
result ~= n;
}
return result;
}
void main() {
auto r = reduce!max(map!((i){return tuple(hailstone(i).length,
i);})(iota(1, 1000)))[1];
writeln(r);
}
With a max that supports a key function and iterables the line of code in the
main() becomes just:
auto r = max!hailstone(iota(1, 1000));
With the enhancement request 5395 it becomes even more readable:
auto r = max!hailstone(1 .. 1000);
--
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