Pull a pull. Pure function

Source: Internet
Author: User

Functional programming has become an increasingly hot topic in the programming circle. The first time I heard that functional programming was in the course of compiling principles, the great young teacher gave us a brief introduction to the ML language at Princeton, when programming and mathematical foundations were so weak that I had a huge fear of the word "function", which was completely incomprehensible to ML's syntax. The first time to realize that functional programming is in which article on the Internet see JS Functional Programming Ability (now this article is everywhere), I found that my most commonly used language originally has this great ability, and then complacent thought oneself is functional programming programmer.

Oh, very silly.

JS is extremely flexible, and indeed has the ability to function programming, but not to write an internal function, the parameters of a function or return function is functional programming, this is only the minimum requirements of functional programming, the so-called function is a first-class citizen.

Then I learned some clojure and I knew there was a problem with the pure impure function.

And then I learned a little. Haskell knows how elegant it can be to call functions for functional programming.

Today is a pure function, elegant tomorrow.

A pure function is a function that does not affect the outside world, it is not just the outside of the program, but the outside of the function. So Haskell has no variables. It was horrible to know the concept at first. No variables!!! Did I not have a place to save the results of my program running to every step? For example, I have a person, he age++, if it is not a variable, even the age can not be + +. Indeed, in pure function, you give the function a 29-year-old person, he will return to you a 30-year-old person, the original 29-year-old is still there. This is not science ah, why no more than a person! Well, I would say that purely functional languages are not object-oriented, so don't think like that. Can think, 30-year-old I am not 29 years old I have ... Very vicissitudes of interpretation-_-!

I think that functional programming is not so easy to accept because it is abstract. Whether you are a person or a wood, for the function is an original to be processed, the function is responsible for producing a new finished product, does not affect the original, buy a gift a bad?

So what's the good of it? It's safe to take Haskell's most triumphant words. You can never change an already existing value, which means that the value is safe for you to use at any time, and can be used by anyone safely, this is not the most difficult problem in concurrency. What is this lock lock, since there is no variable, you don't have to lock it. Not to mention concurrency (as a JS programmer, really can't go into the concurrency), said to write their own linear program, often the error is a value has been changed, but you do not realize, but also in the original way to use it, this problem can be avoided, you have to use the original value is the original value, To use the new value must be a value that has been processed through a function.

Then how pure functional programming language, or to deal with the outside world, otherwise the language will not have the meaning of existence. Pure function of the language will be impure place to carry out, a brain for you to pollute the outside world, instead of constantly polluting the outside world, so you will be more aware of what is done to the outside world.

For example, compare. Using jquery for Dom operation, I like its chain syntax, query out a DOM object began to change it, point to change a little bit,. AddClass add a class name,. css changed a style ... Functional programming is certainly not the case, it will be to get the original DOM object, through a series of functions of processing, the result of each processing is the next processing of raw materials, and the results of each processing will not affect the outside world (this result is not hanging on the page of things), until the final processing results, Only solemnly to actually manipulate the DOM to replace the original elements, the most likely way is to write innerHTML directly.

Well... But that's my guess, because I've never written a web script in a purely functional programming language at the moment. But this language exists, such as Elm, which is a language that eventually generates JS, CSS, and HTML code that resembles Haskell syntax, using a technique called virtual dom that does not directly manipulate the DOM but is called the dummy DOM data. This data will of course not directly change the DOM (so not directly change it but every time the function is called to produce a new virtual DOM is completely possible), when the need to really change the DOM, the system will compare the DOM of the minimum difference, and then make the smallest changes, so performance is also a leverage. How does this fit perfectly with the idea of pure function programming?

Pull a pull. Pure function

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.