Scala Functional programming

Source: Internet
Author: User

1. Functions as values

In Scala, functions are stored in variables like numbers. You can assign a function to a variable in the form: Val Foee=fun _ (function name + space +_)

2. Anonymous functions

In Scala, you don't need to give each function a name, but instead assign the anonymous function to a variable

Example: Array (3,4,5). Map ((x:double) =>3*x)

Array (9,12,15)

Here, we tell the map method to multiply each element by 3

anonymous function General format: (parameter 1, parameter 2, ...) ) =>{function Body (code block)}

3. Functions with Function parameters

Implement a function that accepts another function as a parameter

For example: Def value (f: (Double) =>double) =f (0.25)//The parameter here is any function that accepts double and returns double

Then call: Value (Sqrt_)//0.5

The main source of Spark is the processing of collections. Because value is a function that takes a function argument, it is called a higher-order function.

Higher-order functions can also produce another function, such as:

def mulby (factor:double) = (x:double) =>factor*x

Val Quintuple=mulby (5)

Quintple (20)//100

4. Function return value is also a function

def func (s:string) = (m:string) =>println (s+ ":" +m)

Func ("Spark") returns a function, in the body of the function, for input parameters, only one, the input parameter name can be omitted, with _ (placeholder) instead

For example: Val array=array (1,2,3,4)

Array.map (2*_). foreach (println)//2,4,6,8

5. Closures, operating scopes, closures are composed of any non-restricted variable definitions used by code and code

def mulby (factor:double) = (x:double) =>factor* x

Consider the following call:

Val A=mulby (3)

Val B=mulby (0.5)

println (A (+) + "" +b (14))//42 7

Once the function is executed, the function is executed again, and the internal parameters of the function can still be accessed

6. Currying: A function of n parameters, converted to n functions

For example: def sum (x:int,y:int) =x+y

def sum (x:int) (Y:int) =x+y

7.SVM Conversion

In Scala, whenever you need to tell another function to do something, a function parameter is passed to it. Java does not support functions, and Java programmers need to pay more to achieve the same effect. It is common practice to place an action in a class that implements an interface, and then pass one instance of the class to another method.

To enable this syntax, you need to provide an implicit conversion.

8. Assembly function Programming

The function of the List,set collection is mainly the operation of Val list=list ("Scala", "Spark", "Hadoop") List.map (_) List.map (println) mutable (variable), immutable (immutable ) Scala.collection.Map: Immutable scala.collection.mutable.Map: Variable   Practical example of a functional programming: Count the total number of occurrences of all the words in all the files under a folder. With a space as the delimiter for the word, the test folder has two text files 1.txt,2.txtimport java.io.Fileimport scala.io.Sourceobject secondjob {  def main ( Args:array[string]): Unit = {    val dirfile=new File ("G:/data/test")     Val Files=dirfile.listfiles     for (file <-files) println (file)     Val listfiles=files.tolist    Val Wordsmap=scala . Collection.mutable.map[string,int] ()     Listfiles.foreach (file =>source.fromfile (file). Getlines (). foreach ("Line=>line.split") .                  foreach (    &NBS P                 word=>{                        if (wordsmap.contains (word)) {    &NBSp                     WORDSMAP (word) +=1                       }else {                  &NB Sp       wordsmap+= (word->1)                       &N Bsp }                     }            & nbsp    )             println          Wordsmap &nbsp ; For ((key,value) <-wordsmap) println (key+ ":" +value)  }}

Scala Functional Programming

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.