Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Lazy function of Scala programming

Shulou Source: shulou.com Published: 2022-06-03 07:17:43 10月02日 Update

First, why do you need lazy functions

Lazy evaluation (delaying expression evaluation as much as possible) is a feature of many functional programming languages. Lazy collections provide their elements when needed without the need to pre-calculate them, which brings some benefits. First, you can postpone time-consuming calculations until they are absolutely necessary. Second, you can create unlimited collections that continue to provide elements as long as they continue to receive requests. The lazy use of functions allows you to get more efficient code. Java does not provide native support for inertia, but Scala does, making it easy to use.

2. Lazy loading is realized by java

Lazy style

Public class LazyDemo {private String property; / / attribute may also be a database connection, files and other resources public String getProperty () {if (property==null) {property=initProperty (); / / if it has not been initialized, initialize} return property;} private String initProperty () {return "property";}} III. Introduction to lazy functions

When a function is declared as lazy, the function does not execute immediately, but when we first call the function, we execute it. So we call this a lazy function (lazy loading in java).

IV. Cases

Code 1:

Object LazyDemo {def main (args: Array [String]): Unit = {val res= sum (10Magne20) / / without the modification of lazy, this function is eager's println ("=") / / println ("res:" + res)} def sum (Int= {println ("sum executed") aquib}}

Output: =

Sum has executed

Res:30

Code 2:

Object LazyDemo {def main (args: Array [String]): Unit = {lazy val res= sum (10Magne20) / / lazy modification. The sum () function is not executed immediately, but println ("=") / / println ("res:" + res)} def sum is executed after it is called for the first time: Int= {println ("sum executed")

Output: =

Code 2:

Object LazyDemo {def main (args: Array [String]): Unit = {lazy val res= sum (10Magne20) / / lazy modification. The sum () function will not be executed immediately. The println ("=") println ("res:" + res) / / call to the sum method} def sum (def sum): Int= {println ("sum executed") will not be executed immediately.

Output: =

Sum has executed

Res:30

To sum up, we can see that after being modified by lazy in scala, lazy loading can be realized, which will be very useful in the loading calculation of data in big data's project!

Five: pay attention

1) lazy cannot modify var type variables

2) after the function is modified by lazy, the operation of the function will be delayed. We are declaring a variable. If we add a lazy to the variable, then the declaration of the variable will also be delayed, and the declaration will take effect only when it is used. For example:

Scala > val astat100a: Int = 100scala > lazy val baked 1000b: Int = scala > println (b) 1000

Tags: Functions inertia code variables output elements data time programming only large numbers benefits that is as much as possible attributes slackers databases files methods useful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi NVidia Microsoft vpn