www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Convert little imperative code to functional coding style

reply bioinfornatics <bioinfornatics fedoraproject.org> writes:
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 =3D 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) =3D=3D 1_000 ) writeln((m ^^ 2 - n ^^ 2) =
*
(2 * m * n) * (m ^^ 2 + n ^^ 2));
        //~ }
    //~ }

    auto r =3D iota(2, limit )
        .map!( m =3D> tuple( m, iota( 1, m - 1)
                                .filter!( n =3D> 2 * m * (m+n) =3D=3D 1_000=
 )
                            )
            ).filter!( n =3D> !n[1].empty );

    auto m =3D r.array[0][0];
    auto n =3D 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 =3D> 2 * m * (m+n) =3D=3D 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
Aug 09 2012
next sibling parent bioinfornatics <bioinfornatics fedoraproject.org> writes:
Le jeudi 09 ao=C3=BBt 2012 =C3=A0 18:49 +0200, bioinfornatics a =C3=A9crit =
:
 Dear,
 i try convert a code to functional coding style:
=20
 commented code is what i try to convert to functional
=20
=20
 _______________________________________
=20
 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;
=20
=20
 void main( ){
     immutable size_t limit =3D cast(size_t )
 floor( sqrt( cast(double)1_000 ) );
=20
     //~ foreach( m;  iota( 2, limit ) ){
         //~ foreach( n; iota( 1, m-1) ){
             //~ if( 2 * m * (m+n) =3D=3D 1_000 ) writeln((m ^^ 2 - n ^^ 2=

 (2 * m * n) * (m ^^ 2 + n ^^ 2));
         //~ }
     //~ }
=20
     auto r =3D iota(2, limit )
         .map!( m =3D> tuple( m, iota( 1, m - 1)
                                 .filter!( n =3D> 2 * m * (m+n) =3D=3D 1_0=

                             )
             ).filter!( n =3D> !n[1].empty );
=20
     auto m =3D r.array[0][0];
     auto n =3D r.array[0][1];
     writeln( typeid( n ) );
     writeln(  n  );
     //~ writeln( (m ^^ 2 - n ^^ 2) * (2 * m * n) * (m ^^ 2 + n ^^ 2));
 }
=20
 _______________________________________
=20
=20
 I want to compute (m ^^ 2 - n ^^ 2) * (2 * m * n) * (m ^^ 2 + n ^^ 2)
 when  n =3D> 2 * m * (m+n) =3D=3D 1_000
=20
 I do this in 2 step maybe that is possible in one.
=20
 I search to convert m and n to size_t type
=20
 thanks for your help
=20

by using auto n =3D 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 ?
Aug 09 2012
prev sibling next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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);
}
Aug 10 2012
next sibling parent reply "Nathan M. Swan" <nathanmswan gmail.com> writes:
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 ;)
Aug 10 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 08/10/2012 09:59 PM, Nathan M. Swan wrote:
 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 ;)

In an attempt to destroy that guess I have uncovered the following compiler bug: http://d.puremagic.com/issues/show_bug.cgi?id=8542
Aug 12 2012
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Nathan M. Swan:

 Ugh! I guess functional isn't always the best ;)

There are many situations where I prefer simple procedural code over puzzle-style Haskell code that uses every operator of the Control.Arrow standard module :-) But in this case the code is not too much hard to read. Regarding Timon's code, Code Golfing is fun, but I suggest do do it only on sites like this: http://codegolf.com/ Otherwise I suggest to add spaces around operators, after commas, etc. Bye, bearophile
Aug 10 2012
prev sibling parent bioinfornatics <bioinfornatics fedoraproject.org> writes:
Le vendredi 10 ao=C3=BBt 2012 =C3=A0 20:26 +0200, Timon Gehr a =C3=A9crit :
 Is this what you are looking for?
=20
 import std.stdio;
 import std.range        : iota;
 import std.algorithm    : map, filter, joiner;
 import std.typecons     : tuple;
 import std.math         : sqrt, floor;
=20
 void main(){
      immutable limit =3D cast(size_t)floor(sqrt(1_000.0));
=20
      auto r =3D iota(2,limit).map!(m=3D>iota(1,m-1).map!(n=3D>tuple(m,n))=

              .filter!(t=3D>2*t[0]*(t[0]+t[1])=3D=3D1_000)
              .map!(t=3D>(t[0]^^4-t[1]^^4)*(2*t[0]*t[1]));
=20
      writeln(r.front);
 }
=20

oh yes beautiful thanks
Aug 11 2012