Using C # for functional programming

Source: Internet
Author: User
Tags memory usage

To bring up functional programming, we must think of the highly flexible syntax and dynamic Lisp,haskell such an ancient functional language, near to say ruby,javascript,f# is also a popular language of functional programming. However, since. NET supports lambda expressions, C #, as a command-programming language, is no less functional programming. We use C # in the process of writing code, intentionally or unintentionally using higher-order functions, combination functions, pure function caching and other ideas, even the expression tree idea also comes from functional programming ideas. Therefore, we will make a summary of the commonly used functional programming scenarios, which will help us to apply these technologies flexibly in the process of programming, expand our design ideas and improve the code quality.

First, higher order functions

Higher-order functions are popular: a function is used as a parameter, and such a function is called a higher order function. According to this definition, the LINQ expressions that are used in. NET, such as Where,select,selectmany,first, are high order functions, so when do we use this design when we write our own code?

For example: Design a function to calculate the property cost, Var fee=square*price, and the area (square) according to the nature of the property, the calculation method is different. Civil residential, commercial housing, etc. multiplied by different coefficients, we try to design the following function according to the demand:

Civil residential Area:

Public Func Squareforcivil ()

{

Return (Width,hight) =>width*hight;

}

Commercial residential Area:

Public Func squareforbusiness ()

{

Return (width, hight) => width * HIGHT*1.2M;

}

These functions have a common signature: Func, so we can use this function signature to design a function that calculates a property fee:

Public decimal propertyfee (decimal price,int width,int hight, Func Square)

{

Return Price*square (width, hight);

}

Is it easy to write a test to see

[Test]

public void Should_calculate_propertyfee_for_two_area ()

{

Arrange

var calculator = new Propertyfeecalculator ();

Act

var feeforbusiness= calculator. Propertyfee (2m,2, 2, Calculator. Squareforbusiness ());

var feeforcivil = Calculator. Propertyfee (1m, 2, 2, Calculator. Squareforcivil ());

Assert

Feeforbusiness.should (). Be (9.6m);

Feeforcivil.should (). Be (4m);

}

Second, the lazy evaluation

C # uses a strict evaluation strategy in the execution process, and the so-called strict evaluation refers to the argument being evaluated before it is passed to the function. Is this explanation still a little unclear? Let's look at a scenario where a task needs to be performed, requiring the current memory usage to be less than 80%, and the result of the previous calculation

We can quickly write the C # code that meets this requirement:

Public double memoryutilization ()

{

Calculate Current Memory usage

var pcinfo = new ComputerInfo ();

var usedmem = pcinfo.totalphysicalmemory-pcinfo.availablephysicalmemory;

Return (double) (Usedmem/convert.todecimal (pcinfo.totalphysicalmemory));

}

public int Bigcalculatationforfirststep ()

{

First step operation

System.Threading.Thread.Sleep (Timespan.fromseconds (2));

Console.WriteLine ("Big Calulation");

Firststepexecuted = true;

return 10;

}

public void NextStep (double memoryutilization,int firststepdistance)

{

Next operation

if (memoryutilization

{

Note : For more wonderful tutorials, please pay attention to the Triple Web Design Tutorial section,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.