Convert little imperative code to functional coding style

bioinfornatics bioinfornatics at fedoraproject.org
Thu Aug 9 10:00:24 PDT 2012


Le jeudi 09 août 2012 à 18:49 +0200, bioinfornatics a écrit :
> Dear,
> i try convert a code to functional coding style:
> 
> commented code is what i try to convert to functional
> 
> 
> _______________________________________
> 
> import std.stdio;
> import std.range;
> import std.algorithm;
> import std.conv         : to;
> import std.typecons     : tuple;
> import std.math         : sqrt, floor;
> import std.array        : empty, array;
> 
> 
> void main( ){
>     immutable size_t limit = cast(size_t )
> floor( sqrt( cast(double)1_000 ) );
> 
>     //~ foreach( m;  iota( 2, limit ) ){
>         //~ foreach( n; iota( 1, m-1) ){
>             //~ if( 2 * m * (m+n) == 1_000 ) writeln((m ^^ 2 - n ^^ 2) *
> (2 * m * n) * (m ^^ 2 + n ^^ 2));
>         //~ }
>     //~ }
> 
>     auto r = iota(2, limit )
>         .map!( m => tuple( m, iota( 1, m - 1)
>                                 .filter!( n => 2 * m * (m+n) == 1_000 )
>                             )
>             ).filter!( n => !n[1].empty );
> 
>     auto m = r.array[0][0];
>     auto n = r.array[0][1];
>     writeln( typeid( n ) );
>     writeln(  n  );
>     //~ writeln( (m ^^ 2 - n ^^ 2) * (2 * m * n) * (m ^^ 2 + n ^^ 2));
> }
> 
> _______________________________________
> 
> 
> I want to compute (m ^^ 2 - n ^^ 2) * (2 * m * n) * (m ^^ 2 + n ^^ 2)
> when  n => 2 * m * (m+n) == 1_000
> 
> I do this in 2 step maybe that is possible in one.
> 
> I search to convert m and n to size_t type
> 
> thanks for your help
> 

by using auto n = r.array[0][1].front; i am able to get the result.

So now how cleanuo the code ? to compute only filter is true ? using
until ?



More information about the Digitalmars-d-learn mailing list