Convert little imperative code to functional coding style
Nathan M. Swan
nathanmswan at gmail.com
Fri Aug 10 12:59:22 PDT 2012
On Friday, 10 August 2012 at 18:26:56 UTC, Timon Gehr wrote:
> Is this what you are looking for?
>
> import std.stdio;
> import std.range : iota;
> import std.algorithm : map, filter, joiner;
> import std.typecons : tuple;
> import std.math : sqrt, floor;
>
> void main(){
> immutable limit = cast(size_t)floor(sqrt(1_000.0));
>
> auto r =
> iota(2,limit).map!(m=>iota(1,m-1).map!(n=>tuple(m,n))).joiner
> .filter!(t=>2*t[0]*(t[0]+t[1])==1_000)
> .map!(t=>(t[0]^^4-t[1]^^4)*(2*t[0]*t[1]));
>
> writeln(r.front);
> }
Ugh! I guess functional isn't always the best ;)
More information about the Digitalmars-d-learn
mailing list