Functional programming from closures

Source: Internet
Author: User

I remember that when I started learning golang, boss sent me a paper about functional programming. At that time, I read it again, and I had only one feeling of ignorance, how does function programming seem to talk about mathematical formulas )? Recently, go has been widely used, and business logic has made me think a lot. Although go is not a functional language, it supports many functional concepts, make programming easier. Of course, when I write code, I doubt that using functional programming will lead to lower running efficiency?

 

In this article, we will learn functional programming from several aspects (by Wei Ganen)

1. What is the relationship between mathematical formulas and functional programming?

2. What are the features of functional programming? What concepts does go support?

3. Functional Programming and Operation Efficiency

 

1. What is the relationship between mathematical formulas and functional programming?

For a simple example, there is a concept in mathematics called ing (y = f (x). The common point is the function. The most familiar one is the quadratic function (parabolic y = a * x + B * x + C)

Now coding is used to calculate the value of a certain point on the parabolic curve. We know that A, B, and C are parameters, X is independent variables, and Y is dependent variables, I may do this (to commemorate the C ++ I haven't written for a long time, I 'd like to write it in C ++)

double getParabola(double a,double b,double c,double x) {     return a*x*x+b*x+c;}

Okay, the problem is coming,

Problem 1: Given a parabolic curve, the following method is used to calculate the value of X = 2, x = 3, x = 4.

resultA = getParabola(a,b,c,2) resultB = getParabola(a,b,c,2) resultC = getParabola(a,b,c,2) 

This is a normal practice in the program. However, from the mathematical point of view, is there a way to conform to the mathematical formula thinking? The following is another implementation of mine (Here we use go to implement it, because I know how to write c ++ ),

func getParabola(aa,bb,cc float32){    var a = aa    var b = bb    var c = cc    a := func(x float32) {           return a*x*x+b*x+c    }    return a}

Then, for problem 1, the solution is as follows:

parabola := getParabola(a,b,c)resultA := parabola(2)resultB := parabola(3)resultC := parabola(4)

Is it the same as the function value? Therefore, mathematical relationships are well reflected in functional programming.

2. What are the features of functional programming? What concepts does go support?

Function programming has three main features:

1. Immutable variable: once assigned, the variable cannot be changed. If it needs to be changed, it must be copied and modified. In go, once the string variable is assigned a value, it cannot be modified like C ++, C [2] = 'A', but explicitly converted to [] Byte, then modify. But it is another memory.

2. Function Type first-class citizen: a function is also a variable and can be passed in a program as a parameter or return value. C ++ and go should support this feature.

3. tail recursion: The concept of recursion was learned in the Fibonacci series. If recursion is deep, the stack may crash, resulting in a significant reduction in performance. If the compiler supports the tail recursion optimization technology, stack can be reused at each recursion (tail recursion indicates that the recursive call takes place in the last step. At this time, the previous results are passed as parameters to the last call, so the previous status has no effect, so you can reuse the stack ). Is go supported? I am not sure yet.

Common functional programming techniques

1. Map & reduce & Filter

MAP is used to call the same function for each input to generate an output, such as for_each in C ++, map in hadoop, and map in Python.

Reduce is used to add the previous output to each input to get the next output, such as reduce in Python and hadoop,

Filter is used for filtering, such as count_if of C ++.

2. Recursion

3. Pipeline

Put the function instance in an array or list, and then pass the data to this action list. The input is operated by each function in sequence (meaning the output of each function is used as the input of another function, data is flowing, and computing is fixed, similar to the concept of storm.) Finally, we can get the expected results.

4. Others (for further study)

 

3. Functional Programming and Operation Efficiency

One of the most important concepts of functional programming is functional first-class citizenship. functions are the same as variables. Can be used as a parameter, return value, and so on. I do not agree to use the value assignment statement, so many recursive statements are used, so the efficiency of functional programming is certainly relatively low.

Recently, I have used a lot of closures. The concept of closures is to add a function to an environment (one or more variables) and evaluate the closure expression every time, they all get an isolated result, which is different from a common function. A common function is a piece of executable code. As long as the entry is determined, the call location is determined. For example, the above parabolic example calls

a:=getParabola(0.2,0.1,0.3)b:=getParabola(0.1,0.1,0.4)

Two parabolic results are obtained. I think the efficiency will decrease because the closure itself is a process of value assignment, involving the creation and destruction of variables. Of course, I did not actually test the performance. If Server efficiency is reduced in subsequent releases, this may be a consideration.

 

This article is just a kind of perceptual knowledge about code writing in the recent period and searching for some information on the Internet. As a memo, you can learn it later.

 

Functional programming from closures

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.