In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
I. concept
Closure: we combine a function with its associated reference environment (variables) into a whole called closure
The concept is obscure, so let's use an example to illustrate it.
Small example def minusxy (x:Int) = (y:Int) = > x-yval f=minusxy (20) println ("f (1) =" + f (1)) / / 19println ("f (2) =" + f (2)) / / 18
In minusxy, x is an external variable of an anonymous function, but it is used in an anonymous function, just as it is contained in an anonymous function, so we combine a function with its related reference environment (variables) into a whole called closure.
Explanation:
1) (y: Int) = > xmury
An anonymous function is returned because the function refers to x outside the function, so the function and x as a whole form a closure
For example, the f function of val f = minusxy (20) is the closure.
2) you can understand that the return function is an object, and x is a field of that object, and together they form a closure.
3) when f is called multiple times (it is understandable that the closure is called multiple times), it is found that the same x is used, so the ⅹ remains unchanged.
4) when using closures, it is mainly clear which variables outside the function are referenced by the return function, because they will be combined into a whole (entity) to form a closure.
I'm sure you've understood it through the example above, so try the following requirement
Third, practice your hands
1) write a function makeSuffix (suffix: String) that takes a file suffix (such as .jpg) and returns a closure.
2) call closure, and you can pass in a file name
If the file name does not have a specified suffix (such as .jpg), the file name .jpg is returned. The file name is dog = "dog.jpg"
If there is already a .jpg suffix, the original file name is returned. The file name is cat.jpg = > cat.jpg
3) it is required to use closures.
Tip: String.endsWith (xx)
= pretend to have a dividing line =
Object ClosureDemo {def main (args: Array [String]): Unit = {val f=makeSuffix (".jpg") println (f ("cat")) println (f ("dog.jpg"))} / * * closure * @ param suffix variable * @ return * / def makeSuffix (suffix:String) = (fileName:String) = > {if (fileName.endsWith (suffix) fileName else fileName+suffix}} IV. Benefits of closure
From the little exercise above, we know that if we use the traditional method, we can easily achieve this function, but the traditional method needs to pass in the suffix every time, such as .jpg, and the closure can be used repeatedly because it can retain a value referenced last time.
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.