High-order functions of the Scala language (4)

Source: Internet
Author: User
Tags closure

==> Common function explanation

---> Map acts on each element of the list

Define a list of Val list= list (1,2,3,4,5,6,7,8,9)//list all elements in the list by multiplying 2list.map ((i:int) + i*2)//using foreach to do the same operation, foreach does not return Value List.foreach ((i:int) = i*2)


---> Filter filters return an element with a value of False

Val list = list (1,2,3,4,5,6,7,8,9) List.filter ((i:int) = i%2==0)//Return result: list[int] = List (2, 4, 6, 8)//can also write Num.filter ( _%2==0)


---> Zip combines two numbers and

List (4,5,6)///output is list[(int, int)] = List ((1,4), (2,5), (3,6))


---> Partition place elements that conform to the filtering rules in one partition, placing them in a partition that does not conform to the filter rules

List (1,2,3,4,5,6,7,8,9). partition ((I:int) and i%2==0)//Output results are: (List[int], list[int]) = (List (2, 4, 6, 8), List (1, 3, 5, 7 , 9))


---> Find finds the first rule-compliant element in a list

< Strong style= "Color:rgb (31, 73, 125); White-space:normal; " >

list (List (1,list), 2,3,4 (list ( list), List (5,6,7)). 8,9   If you want to continue the flattening operation, you will get an error List (list (1,list (2,3,4)),  list (list (5,6,7), List (8,9)) .flatten.flattenerror:< console>:12: error: no implicit view available from any =>  SCALA.COLLECTION.GENTRAVERSABLEONCE[B]. List (list (1,list (2,3,4)),  list (list (5,6,7), List (8,9)) .flatten.flatten//  to continue the flattening operation, you need to modify the processing process to List ( List (list (1), List (2,3,4)),  list (list (5,6,7), List (8,9))) .flatten.flatten//  result:list[int] =  List (1, 2, 3, 4, 5, 6, 7, 8, 9) 

< Strong style= "Color:rgb (31, 73, 125); White-space:normal; " >

---> FLATMAP equivalent to a combination of two functions of Map and flatten

Val list = list (4,5,6)//The x here is equivalent to flatten the source data, then operates on each element List.flatmap (X=>x.map (_*2))// This clause is equivalent to the following two-step operation val x = List.flattenx.map (_*2)



Nesting of ==> closure functions

---> Define common functions:

def myfun (X:int, y:int): Int = x * y


---> use closures

Define a closure function, where x is a multiplier factor, which is multiplied by multiples (I understand it as a multiplier), Y is the parameter passed in using the function Def myfun (x:int) = (y:int) + x * y//First step defines the model of its own function val mytest = Myf UN (2)//Use your own defined function model, pass in Parameter mytest (20)


==> currying The name of a mathematician whose essence is to convert a multi-parameter function into a function chain with a single parameter function on each node

Example:

def add (X:int, y:int): Int = x + ydef Add (x:int) (Y:int) = x + y


High-order functions of the Scala language (4)

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.