What is the advantage of Currying in C#? (achieving partial function)

后端 未结 5 2072
情深已故
情深已故 2021-02-07 09:13

What is the advantage of Currying in C#?

What is the advantage of achieving partial function application on a curried function?

5条回答
  •  遇见更好的自我
    2021-02-07 09:52

    A simple Currying will be

    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Func newTonLaw = (m1, m2, r, g) => ((m1 * m2) / Math.Pow(r,2)) * g;
    
                // Mass of Earth= 5.98 * 10e24 , Gravitational Constant = 6.6726 * 10e-11
                Func onEarth = (m2, r) => newTonLaw.Invoke(5.98 * 10e24, m2, r, 6.6726*10e-11);
    
                // Mass of Moon= 7.348x10e22 , Gravitational Constant = 6.6726 * 10e-11
                Func onMoon = (m2, r) => newTonLaw.Invoke(7.348 * 10e22, m2, r, 6.6726 * 10e-11);
    
                Trace.WriteLine(onEarth(70, 6.38 * 10e6)); // result 686.203545562642
                Trace.WriteLine(onMoon(70, 6.38 * 10e6)); // result 8.43181212841855
            }
        }
    }
    

提交回复
热议问题