Features of functional languages

Source: Internet
Author: User
Tags hadoop wiki

Functional languages, of course, have the following features:

    • Higher order functions (Higher-order function)
    • Partial application function (partially applied Functions)
    • Curry (currying)
    • Closures (Closure)

Higher-order functions are functions that have parameters as functions or return values as functions. With higher-order functions, the granularity of reuse can be reduced to the function level, and the granularity of reuse is lower relative to the object-oriented language.

For example, suppose the following three functions,

DefSumints(A:Int,B:Int):Int=If(A>B)0ElseA+Sumints(A+1,B)DefSumcubes(A:Int,B:Int):Int=If(A>B)0ElseCube(A)+Sumcubes(A+1b) def sumfactorials  (a: intb: int) : int = if  (a > b) 0 else fact (a+ sumfactorials (a + 1b)         

is to seek the sum of integers between A and B, the cubic sum of integers between A and B, and the factorial sum of integers between a and B.

In fact, these three functions are the special case of the following formula

The three functions differ only in the F difference, so is it possible to abstract a common pattern?

We can define a high-order function sum:

DefSum(F: int => inta: intb: int : int = if  (a > b) 0 else f (a+ sum (fa + 1b)         

Where parameter f is a function, the F function is called in the function to calculate and sum.

Then you can write the following function

DefSumints(A:Int,B:Int)=Sum(Id,A,B)DefSumcubs(A:Int,B:Int)=Sum(Cube,A,B)DefSumfactorials(A:Int,B:Int)=Sum(Fact,A,B)DefId(X:Int):Int=Xdef cube (xInt ) : int = x * x * xdef fact ( x: int) : int Span class= "o" >= if  (x == 0) 1 else fact (x -1)           

This allows you to reuse the SUM function to implement the summation logic in three functions.
(Example Source:https://d396qusza40orc.cloudfront.net/progfun/lecture_slides/week2-2.pdf)

Higher-order functions provide a mechanism for dependency injection (or inversion control) at the function level, in which the logic of the SUM function relies on the logic of the injected function. Many GOF design patterns can be implemented using higher-order functions, such as Visitor,strategy,decorator. For example, the visitor pattern can be replaced with a map () or a foreach () high-order function of the collection class.

Functional languages often provide a very powerful collection class (Collection), which provides many higher-order functions, so it is very convenient to use.

For example, we want to multiply each integer in a list by 2, in imperative programming we need to loop through, and then multiply 2 for each element, but in functional programming, we don't need to use loops, just the following code:

Scala>ValNumbers=List(1,2,3,4)Numbers:List[Int]=list (12, Span class= "Mi" >34) scala> span class= "n" >numbers. Map (x=>x*2 ) res3: list[ = list (2468  

(Example Source: Programming Scala:tackle multi-core complexity on the introduction of the Java Virtual machine book)

Where x=>x*2 is an anonymous function that receives a parameter x, the output x*2. It can also be seen here that functional programming focuses on what to do (x*2), rather than on how to do it (using the loop control structure). The programmer does not care at all whether the elements in the list are calculated from the front to the back, from the back to the front, in sequential or parallel computations, such as Scala's parallel collection (Parallel collection).

Using the method of the collection class, you can make it easier for some processing, such as the above-mentioned factorial function, if you use the collection class, you can write:

def fact(n: Int): Int = (1 to n).reduceLeft((acc,k)=>acc*k)

where (1 to N) generates an integer sequence, and the Reduceleft () higher order function will serialize the simplification by calling the anonymous function.

So, in the big Data processing framework spark, an RDD is a collection. Take the word frequency statistics for example code as follows:

ValFile=Spark.Textfile("hdfs://...")Valcounts = file. Flatmap (line => line. Split ( ". Map (word =>  (word 1. Reducebykey (_ + _)  Counts. Saveastextfile ( "hdfs://..." )       

(Example Source:https://spark.apache.org/examples.html)

The example Flatmap (), map (), and the same name method in the collection class are consistent, and the parameters of the map method here are an anonymous function that turns the word into a tuple. The person writing this function does not care about how the function is dispatched, but in fact, the spark framework does this on a distributed cluster of multiple computers.

In addition, if you compare the word frequency statistic implementation of Hadoop: Wordcount-hadoop Wiki, you can see some advantages of functional programming.

The functional programming language also provides lazy evaluation (lazy evaluation, also known as Call-by-need), which is not evaluated when the expression is assigned to a variable (or binding), but only when the variable is used for the first time. This allows performance to be improved by avoiding unnecessary evaluation. In Scala, specifying a variable with lazy Val is lazy, as shown in the following example:

https://www.zhihu.com/question/28292740

Features of functional languages

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.