How to Implement functional programming

Source: Internet
Author: User

How to Implement functional programming

Someone asked me over the weekend how to implement functional programming. My answer is: Use your current language to compile pure functions.

For pure functions, its unique input is its parameter list, and its unique output is its return value. If you have never accepted this concept, you may mistakenly assume that all functions are pure functions, because all functions are output based on input.

However, traditional programming usually involves passing information in other ways. For example, a non-functional function may depend on an external variable or write data to the database. In this way, the function has a side effect other than its return value.

You can perform functional programming in any language, though this method is difficult in different languages. For example, no one would like to say that FORTRAN is a functional language, but many people prefer to use FORTRAN to compile functional languages.Program.

Why do we need to write pure functions without side effects? Pure function parameters are transparent, which means that when the same input is used, it will get the same output. Function output is not affected by system time, database status, and many other factors that are not in the parameter list. This means that pure functions will be easier to understand, debug, and test.

However, you cannot write all functions as pure functions. If you need to place a piece of data in the database, it will be difficult to implement it through pure functions. If you are accessing a random number generator, you certainly do not want it to always have a definite output. Writing all functions as pure functions is not displayed. Some people think that 85% of pure functions in a program are more appropriate.

Why do people do not like pure functional functions?

On the one hand, functional functions are complicated in the list of function parameters. In an object-oriented language, object methods can change according to the object state. The cost of passing simple parameters is that you will not know exactly what the method is to perform, because it depends on your environment and status. I prefer to avoid using too many pure functions by using clear and complete function names.

Another important reason is that people do not want to transfer too many parameters between functions, but pointers can solve this problem. You can pass the pointer of an object to a function to avoid copying parameters.

Maybe you will refuse to use pure functions because of efficiency. For example, Mike talked about the recent use of the memory function to significantly improve program performance. A function that uses the memory function does not meet the functional requirements. However, this function is transparent and always produces the same output when it has the same input. You may think that this type of function cannot be classified into functional functions, but it does. If you must stick to your opinion, all functions have side effects.

 

 

[Original]

 

How to get started with functional programming

By John on July 24,201 1

Someone asked me this weekend how to get started with functional programming. My answer: Start By writing pure functions in the programming language you're currently using.

The only input to a pure function is its argument list and the only output is its return value. if you haven't seen this before, you might think all functions are pure. after all, any function takes in values and returns a value. but in conventional
Programming there are typically out-of-band ways for information to flow in or out of a function. for example, an impure function may depend on a global variable or class member data. in that case, it's behavior is not entirely determined by its arguments.
Similarly, an impure function might set a global variable or write to a database. In that case the function has a side effect in addition to its return value.

You can write pure functions in any language, though it's easier in some languages ages than others. For example, no one wocould call Fortran a functional language, but there are people (M.
J. D. Powell comes to mind) Who discipline themselves to write pure functions in FORTRAN.

Why write pure functions? A pure function has referential transparency, meaning it will always return the same value when given the same inputs. the output does not depend on the system time, the state of a database, which functions were called
Previusly, or anything else that is not explicitly passed as an argument to the function. This means pure functions are easier to understand (and hence easier to debug and test ).

you can't always write pure functions. if you need to stick a value in a database, this cannot be accomplished with a pure function. or if you're calling a random number generator, you don't want it to have referential transparency, always returning
the same output! But the goal is to use pure functions when practical. you want to eliminate out-of-band communication when it's convenient to do so. total purity is not practical; some argue that the sweet spot is about
85% purity.

SO why don't programmers use pure functions more often? One reason is that pure functions require longer argument lists. in an object oriented language, object methods can have shorter argument lists by implicitly depending on object state.
the price to pay for shorter method signatures is that you can't understand a method by itself. you have to know the state of the object when the method is called. is it worthwhile to give up referential transpa Rency in order to have shorter method signatures?
it depends on your context and your taste, though in my opinion its often worthwhile to use longer function signatures in exchange for more pure functions.

another reason people give for not writing pure functions is that its too expensive to copy large data structures to pass them into a function. but that's what pointers are. you can conceptually pass an object into a function without having
to actually make a copy of the object's bits.

You can also fake purity for the sake of efficiency. For example, Mike Swaim left

Comment recently giving an example of how memoization sped up a program by several orders of magn.pdf. (memoization is a technique of caching computations. when a function is asked to compute something, it first looks to see whether it has already done
The calculation. if so, it returns the cached value. if not, it does the calculation and adds its output to the cache .) a function that uses memoization is not strictly pure-its calculations have a persistent impact on the state of its cache-but such
Function can still have referential transparency, always returning the same output given the same input. you cocould say it's cheating to call such functions pure, and it is, but if you're really a stickler about it, all

Pure functions have side effects.

 

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.