Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Big data, a good programmer, shares high-order functions in the learning route.

2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Good programmer big data learns to share high-order functions. We usually call expressions that can be passed as parameters to methods as functions.

Higher-order functions include: functions as values, anonymous functions, closures, Corialization, and so on.

Format when defining a function: val variable name = (input parameter type and number) = > function implementation and return value type and number

"=" means to assign a function to a variable

The left side represents the input parameter name, type and number, and the right side represents the implementation of the function and the return value type and the number of parameters.

A function as a value

Define function

Scala > val func = (x:Int) = > x * x

Func: Int = > Int =

Scala > val func:Int = > Int = x = > x * x

Func: Int = > Int =

Scala > func (3)

Res0: Int = 9

Function call

Scala > val arr = Array (1, 2, 3, 4)

Arr: Array [Int] = Array (1,2,3,4)

Scala > val res = arr.map (x = > func (x))

Res: Array [Int] = Array (1,4,9,16)

Scala > val res = arr.map (func _)

Res: Array [Int] = Array (1,4,9,16)

Scala > val res = arr.map (func)

Res: Array [Int] = Array (1,4,9,16)

Convert a method to a function

Scala > def M1 (x:Int): Int = x * x

M1: (X: Int) Int

Scala > def M1 (x:Int) = x * x

M1: (X: Int) Int

Scala > def m2 (x:Int) {x * x}

M2: (X: Int) Unit

Scala > val F1 = M1 _

F1: Int = > Int =

Scala > val res = arr.map (x = > M1 (x))

Res: Array [Int] = Array (1,4,9,16)

Scala > val res = arr.map (M1 (_))

Res: Array [Int] = Array (1,4,9,16)

Scala > val res = arr.map (M1)

Res: Array [Int] = Array (1,4,9,16)

Anonymous function

In Scala, you don't need to name every function. A function that doesn't assign a function to a variable is called an anonymous function.

Scala > arr.map ((x:Int) = > x * x)

Res3: Array [Int] = Array (1,4,9,16)

Scala > arr.map (x = > x * x)

Res4: Array [Int] = Array (1,4,9,16)

Scala > arr.map (M1)

Res1: Array [Int] = Array (1,4,9,16)

Scala > arr.map (_ * 2)

Res2: Array [Int] = Array (2,4,6,8)

Closure

A closure is a function that can read internal variables of other functions.

It can be understood as a function defined inside a function.

In essence, a closure is a bridge that links the inside and outside of a function.

Object Bibao {

Def sum (f:Int = > Int): (Int,Int) = > Int = {

Def sumf (Int Int.): BRV = {

If (a > b) 0 else f (a) + sumf (a + 1)

}

Sumf / / implicitly converted to a function

}

Def main (args: Array [String]): Unit = {

Def sumInts = sum (x = > x)

Println (sumInts (1pm 2))

}

}

Corey

Coriarization refers to the process of changing a method or function that originally receives two parameter lists into a new parameter list.

Declaration and conversion

Scala > def curry (x:Int) (y:Int) = x * y / / declare

Curry: (X: Int) (y: Int) Int

Scala > curry (3) (4)

Res8: Int = 12

Scala > val curry1 = curry (3) / / conversion method: add ""

Curry1: Int = > Int =

Scala > curry1 (5)

Res9: Int = 15

Scala > def curry2 (x:Int) = (y:Int) = > x * y / / declare

Curry2: (X: Int) Int = > Int

Scala > val func = curry2 (2) / / Direct conversion

Func: Int = > Int =

Scala > func (4)

Res16: Int = 8

Scala > def curry3 () = (x:Int) = > x * x

Curry3: () Int = > Int

Scala > val func = curry3 () / / convert empty parameter

Func: Int = > Int =

Scala > func (3)

Res17: Int = 9

Corialization needs to be combined with implicit transformation

Implicit implicit-> implicit values can only be defined once in the current session of the same type, and different types can define multiple

Scala > def M1 (x:Int) (implicit y:Int=5) = x * y

M1: (X: Int) (implicit y: Int) Int

Scala > M1 (3)

Res10: Int = 15

Scala > M1 (3) (6) / / implicit value can be changed

Res11: Int = 18

Scala > implicit val x = 100 / / is defined as a global implicit value that can be overridden

X: Int = 100

Scala > M1 (3) (6)

Res12: Int = 18

Scala > M1 (3)

Res13: Int = 300

Scala > implicit val y = "abc"

Y: String = abc

Case: add the value of Yuanzu in the array

Scala > val arr = Array (("xixi", 1), ("", 2), ("heihei", 3))

Arr: Array [(String, Int)] = Array ((xixi,1), (,2), (heihei,3))

Scala > arr.foldLeft (0) (+. _ 2) / / (initial value) (last calculation result + loop result)

Res15: Int = 6

Demo of Curry

Object Context {/ / normally creates a new class, and then calls the

Implicit val a = "yaoyao"

Implicit val b = 100

}

Object Curry {

/ / has nothing to do with variables. The system matches values of the same type.

Implicit val a = "yaoyao"

Implicit val b = 100

Def M1 (str:String) (implicit name:String= "xiaodan") {

Println (str + name)

}

Def main (args: Array [String]): Unit = {

Import Context.a

M1 ("Hello")

}

}

Implicit conversion

Function: implicitly enhance the methods of the class to enrich the functionality of the existing class library

Implicit conversion:

Inherit-> enhance the methods of the parent class through method rewriting

Agent mode-> remote agent, mostly used for websites, proxy an instance, can enhance the instance method, before calling the method

Proxy, surround after the method

Decoration mode-> decoration mode is also called wrapper mode. IO stream is used when reading files with java, which is also an enhancement to the instance method.

New BufferInputStream (new FileInputStream) .read ()

Is to use the decoration mode and facade mode->

The decoration mode is the display packaging, and the implicit conversion is the implicit packaging.

The facade mode plays the role of implicit packaging.

Implicit conversion function: a function with a single argument declared with the implicit keyword

Case: implicit conversion is used to read the contents of a file by calling the read method directly from a given URI

Object MyPredef {

/ / implicit def fileToRichFile (file:String) = new RichFile (file)

Implicit val fileToRichFile = (file:String) = > new RichFile (file)

}

Object RichFile {

Def main (args: Array [String]): Unit = {

Val file = "e://wordcount.txt"

/ the display implements the method enhancement of file

/ / val richFile = new RichFile (file)

/ / val content: String = richFile.read ()

/ / println (content)

/ / implicit conversion

Import MyPredef.fileToRichFile

Val content = file.read ()

Println (content)

}

}

Class RichFile (val file:String) {

/ / create read method

Def read (): String = {

Source.fromFile (file). MkString

}

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report