Scala | Lazy Evaluation Last Updated : 03 Mar, 2022 Comments Improve Suggest changes 3 Likes 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. Create Quiz Comment S SrjSunny Follow 3 Improve S SrjSunny Follow 3 Improve Article Tags : Computer Subject Scala Explore OverviewScala Programming Language3 min readIntroduction to Scala7 min readSetting up the environment in Scala3 min readHello World in Scala2 min readBasicsScala Keywords2 min readScala Identifiers3 min readData Types in Scala3 min readVariables in Scala3 min readControl StatementsScala | Decision Making (if, if-else, Nested if-else, if-else if)5 min readScala | Loops(while, do..while, for, nested loops)5 min readBreak statement in Scala3 min readScala | Literals4 min readOOP ConceptsClass and Object in Scala5 min readInheritance in Scala5 min readOperators in Scala11 min readScala Singleton and Companion Objects3 min readScala Constructors4 min readScala | Polymorphism5 min readScala | Multithreading3 min readScala this keyword2 min readMethodsScala | Functions - Basics3 min readAnonymous Functions in Scala2 min readScala | Closures3 min readRecursion in Scala4 min readMethod Overloading in Scala5 min readMethod Overriding in Scala8 min readLambda Expression in Scala4 min readScala Varargs2 min readStringsScala String4 min readScala | String Interpolation3 min readScala | StringContext2 min readRegular Expressions in Scala5 min readStringBuilder in Scala4 min readScala PackagesPackages In Scala4 min readScala | Package Objects3 min readChained Package Clauses in Scala3 min readFile Handling in Scala3 min readScala TraitScala | Traits7 min readScala | Sealed Trait4 min readScala | Trait Mixins3 min readTrait Linearization in Scala5 min readCollectionsScala Lists5 min readScala ListBuffer6 min readListSet in Scala6 min readScala Map5 min readScala | Arrays6 min readScala | ArrayBuffer4 min readScala | Tuple5 min readSet in Scala | Set-13 min readSet in Scala | Set-27 min readBitSet in Scala5 min readHashSet In Scala4 min readStack in Scala3 min readHashMap in Scala3 min readTreeSet in Scala4 min readIterators in Scala5 min readScala | Option3 min read Like