In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the meaning of closures and Corialization in Scala". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the meaning of closures and Corialization in Scala"?
First of all, it is necessary to distinguish the difference between Scala functions and methods, which are two different concepts in Scala. Only by understanding these two concepts can we understand Corialization. Method
Scala > def add (x:Int, y: Int) = x + y
Add: (X: Int, y: Int) Int
Scala > add (1,2)
Res0: Int = 3
Function
Scala > val add_f = (x: Int, y: Int) = > x + y
Add_f: (Int, Int) = > Int =
From the content, we can see that add_f is a function Function.
Scala > add_f (1,2)
Res1: Int = 3
First of all, you should know that the content to the right of the = sign (x: Int, y: Int) = > x + y is a function body
Methods can only be received with def, and functions can be received with either def or val.
When a function is received with def, it is no longer displayed as function, but is converted to a method
Method can omit parameters, but functions cannot. Function can be used as an argument to a method.
Scala > val a = () = > 100
A: () = > Int =
Scala > val a = = > 100
: 1: error: illegal start of simple expression
See here: val a = = > 100 / / an error is reported when the function argument is empty
Understanding closure
Scala > def add (x:Int) = (y:Int) = > x + y
AddBase: (X: Int) Int = > Int
(y:Int) = > x + y is a function body with curly braces left and right omitted!
Add can be understood as a method that returns a value as a function
When a specific parameter is given to a method, a specific function is returned. When the method parameter is different, the returned function is also different. For example
Look at the following:
Scala > val addOne = add (1)
AddOne: Int = > Int =
Scala > addOne (3)
Res2: Int = 4
Let's take another look:
Scala > val addTwo = add (2)
AddTwo: Int = > Int =
Scala > addTwo (3)
Res3: Int = 5
At this point, the concept of closure can be introduced.
You can refer to the methods of external local variables in blocks, and show that blocks are not only simple code, but also include the external "environment". Blocks like this are called closures. The usual local variable does not exist at the end of method execution, but if it is included in the closure, the local variable will exist for the duration of the closure.
In other words, the function body is affected by the external environment, and a closed block of code includes the external environment (the context outside the function), which is the closure.
Finally, it is mentioned that Corey
Coriarization refers to the process of changing the original method of accepting N parameters into a new function that accepts one parameter.
In fact, the code of the above closure is the process of Corialization. The following is the second way to write it.
Scala > def add (x:Int) (y:Int) = x + y
Add: (X: Int) (y: Int) Int
Scala > add (2) (3) / / try calling directly
Res5: Int = 5
Try the Corey call and continue with the following
Scala > val addOne = add (1) _
AddOne: Int = > Int =
Scala > addOne (3)
Res6: Int = 4
Continue with the following
Scala > val addTwo = add (2) _
AddTwo: Int = > Int =
Scala > addTwo (3)
Res7: Int = 5
Extend the Corey process above
(personally understand that Corialization is a bit similar to compound functions):
Scala > def add (x:Int) (y:Int) (z:Int) = {x+y+z}
Add: (X: Int) (y: Int) (z: Int) Int
Scala > add (10) (20) (30)
Res1: Int = 60
/ / the return value is understood as a function
Scala > val addOne = add
AddOne: Int = > (Int = > Int) = $$Lambda$1131/181022659@36df4c26
/ / the return value is understood as a function
Scala > val addTwo = addOne
AddTwo: Int = > Int = $$Lambda$1134/1397187309@6c421123
/ / the return value is no longer a function
Scala > val sum = addTwo
Sum: Int = 600
Baidu encyclopedia definition: in computer science, Currying is a technology that transforms a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the initial function) and returns a new function that accepts the remaining parameters and returns the result. This technique was named by Christopher Strachey after logician Haskell Curry, although it was invented by Moses Schnfinkel and Gottlob Frege.
From a mathematical point of view, this is a process of solving the elimination of a function:
Def f (XRV Intrect YRU Int) = xANGYY
Def g (x:Int) = f (XJI 1)
Def zigg (1)
Zhen2
Then z can also be written like this: def z = (x:Int) = > (y:Int) = > x
At this point, I believe you have a deeper understanding of "what closures and Coriarization in Scala mean". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.