Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Closures for Scala programming (closure)

Shulou Source: shulou.com Published: 2022-06-03 04:48:53 10月04日 Update

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.

Tags: Closures functions files file names variables suffixes totals examples traditions objects that is methods concepts environments obscure above split lines functions benefits fields Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia Shulou Technology MySQL Huawei