Thread overview
Convert little imperative code to functional coding style
Aug 09, 2012
bioinfornatics
Aug 09, 2012
bioinfornatics
Aug 10, 2012
Timon Gehr
Aug 10, 2012
Nathan M. Swan
Aug 10, 2012
bearophile
Aug 12, 2012
Timon Gehr
Aug 11, 2012
bioinfornatics
August 09, 2012
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

August 09, 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 ?

August 10, 2012
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);
}

August 10, 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 ;)

August 10, 2012
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
August 11, 2012
Le vendredi 10 août 2012 à 20:26 +0200, Timon Gehr a écrit :
> 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);
> }
> 

oh yes beautiful thanks

August 12, 2012
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