Scala | Lazy Evaluation Last Updated : 03 Mar, 2022 Comments Improve Suggest changes Like Article Like Report Lazy evaluation or call-by-need is a evaluation strategy where an expression isn't evaluated until its first use i.e to postpone the evaluation till its demanded. Functional programming languages like Haskell use this strategy extensively. C, C++ are called strict languages who evaluate the expression as soon as it's declared. Then there are languages like Scala who are strict by default but can be lazy if specified explicitly i.e. of mixed type. Let's see an example in Scala: Without lazy: val geeks = List(1, 2, 3, 4, 5) val output = geeks.map(l=> l*2) println(output) The value of output is calculated as soon as the operation is applied on it. With lazy: val geeks = List(1, 2, 3, 4, 5) lazy val output2 = geeks.map(l=> l*5) println(output2) The value isn't calculated till we use output2 that's till println(output2). Why lazy evaluation? In the example what if we never use the output value? We wasted our map operation (CPU computations) which can be very costly when we write more complex and bigger code. Here lazy evaluation helps us in optimizing the process by evaluating the expression only when it's needed and avoiding unnecessary overhead. Pros: Optimizes the computation process. Spark a big data computation engine uses this technique at it's core.Lazy evaluation can help us to resolve circular dependencies.Gives access to infinite data structure.Allows modularity of code into parts.The programmer lose control over the sequence their code is executed as some expressions are evaluated and others aren't depending on the need. Cons: Finding bugs can be tricky as programmer has no control over program execution.Can increase space complexity as all the instructions(operations) have to stored.Harder to code in contrast with conventional approach. Comment More infoAdvertise with us Next Article Scala | Lazy Evaluation S SrjSunny Follow Improve Article Tags : Computer Subject Scala Similar Reads ListMap in Scala Immutable maps Implemented by using a list-based data structure. The Scala List class holds a sequenced, linear list of items. We must import scala.collection.mutable.ListMap for ListMap. ListMap collection used only for a small number of elements.Syntax: var listMapName = ListMap("k1"->"v1", "k2 3 min read Pure Function In Scala In any programming language, there are two types of functions: 1. PURE FUNCTIONS 2. IMPURE FUNCTIONSThere is a basic difference between the two, that is pure function doesnât change the variable itâs passed and an impure function does. For example, there exists a function which increases the input b 3 min read Scala Long ==(x: Long) method In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The ==(x: Long) method is utilized to return true if this value is equal to x, false otherwise. Method Definition - def ==(x: Long): Boolean Returns - Returns true if this value is equal to x, false otherwi 1 min read Interesting fact about Scala Scala(pronounced as "skah-lah") is general-purpose programming language designed by Martin Odersky. The design of Scala started in 2001 at EPFL, Lausanne, Switzerland. Scala was released publicly in 2004 on Java platform. Scala is designed to be concise and addresses criticisms of Java. Scala source 3 min read Scala | Methods to Call Option The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null. There are a few methods that 5 min read Like