On Friday, 4 September 2015 at 12:06:08 UTC, Edwin van Leeuwen
wrote:
On Friday, 4 September 2015 at 11:50:23 UTC, deed wrote:
import std.algorithm, std.range, std.array, std.string,
std.stdio,
std.conv;
int[] arr1 = [1, 2, 30];
//arr1.max.writeln; // Doesn't work, as you say
arr1.reduce!max.writeln; // This does. Prints 30.
Again using reduce is the functional way to do it. The above
basically boils down to:
int[] arr1 = [1, 2, 30];
int maxElement = arr1[1];
foreach( element; arr1[2..$] ) //2..$ is short hand for second
till last ($) element
{
maxElement = max( maxElement, element );
}
writeln( maxElement );
Sorry been using too much R, so my indexes are off by 1:
int[] arr1 = [1, 2, 30];
int maxElement = arr1[0];
foreach( element; arr1[1..$] ) //1..$ is short hand for second
till last ($) element
{
maxElement = max( maxElement, element );
}
writeln( maxElement );