Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to calculate func in c#?

I have this:

eight(minus(three())); should return 5
eight(plus(three())); should return 11

I only added two functions:

private static Func<int> Minus(
       Func<int> left,
       Func<int> right)
       => left - right;

private static Func<int> Plus(
       Func<int> left,
       Func<int> right )
       => left + right;

How can I solve the problem?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This should work

    double One( Func<double>? right=null)=> 1 + (right!=null ? right(): 0 );
    double Two( Func<double>? right=null)=> 2 + (right!=null ? right(): 0 );
    double Three( Func<double>? right=null)=> 3 + (right!=null ? right(): 0 );
    double Four( Func<double>? right=null)=> 4 + (right!=null ? right(): 0 );
    double Five( Func<double>? right=null)=> 5 + (right!=null ? right(): 0 );
    double Six( Func<double>? right=null)=> 6 + (right!=null ? right(): 0 );
    double Seven( Func<double>? right=null)=> 7 + (right!=null ? right(): 0 );
    
    double Eigth(Func<double>? right =null)=> 8 + (right != null ? right() : 0);
    double Nine(Func<double>? right =null)=> 9 + (right != null ? right() : 0);
    double Zero(Func<double>? right =null)=> 0 + (right != null ? right() : 0);
    
    
    Func<double> Plus(double right) => () => right;
    Func<double> Minus(double right) => () => right * -1;
    
    
    
    Console.WriteLine(Eigth(Minus(Three())).ToString()); // 5 
    Console.WriteLine(Eigth(Plus(Three())).ToString()); // 11
    
    Console.WriteLine(One(Minus(Three())).ToString()); // -2 
    
    Console.WriteLine(One(Plus(Four())).ToString()); // 5
    Console.WriteLine(One(Plus(Zero())).ToString()); // 1
    Console.WriteLine(Two(Plus(Five())).ToString()); // 7
    
    Console.WriteLine(Seven(Minus(Six())).ToString()); // 1
    Console.WriteLine(Nine(Plus(Six())).ToString()); // 15
    Console.Write(Seven(Plus(Nine(Plus(Two()))))); // 18 
    Console.WriteLine(Two(Plus(Nine(Minus(One(Plus(Seven()))))))); // 2 + ( 9 - (1 + 7) ) = 3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading