Getting Lazy With Scala

In this blog, we will talk about lazy evaluation in Scala. How we can add efficiency to our application?

Efficiency is achieved not just by running things faster, but by avoiding things that shouldn’t be done in the first place.

In functional programming, lazy evaluation means efficiency.  Laziness lets us separate the description of an expression from the evaluation of that expression. This gives us a powerful ability—we may choose to describe a “larger” expression than we need, and then evaluate only a portion of it. There are many ways to achieve lazy evaluation in Scala i.e using lazy keyword, views, streams etc.

For example,

Here, if you notice, when I defined val naming num, it has value 3. But when I defined another val named lazyNum with a lazy keyword,  it has no value because it will be computed lazily.  The difference between them is, that a val is executed when it is defined whereas a lazy val is executed when it is accessed the first time. So when we used again lazyNum, it has value 3 now.

In contrast to a method (defined with def), a lazy val is executed once and then never again. This can be useful when an operation takes a long time to complete and when it is not sure if it is later used.

Scala supports two types of collections:

1) Strict Collection i.e List, Map, Set etc

2) Non-Strict Collections i.e Streams

Strict collection means that they all are eagerly evaluated i.e List, Set, Vector, Map etc.

For example, if we create a list of 10 elements, memory is allocated for all those elements immediately.

But, non-strict collections like streams, are by default lazy evaluated. They are evaluated on demand. For example, if I create a stream of 10 numbers it won’t create it.

Here, head of lazyList is eagerly computed but the tail has not computed yet.

Streams

A stream is a collection like a list except that it is lazily evaluated. That’s why we can have infinite elements in a stream.   In streams, elements are being computed on demand. We create a list using cons :: operator, similarly, we build stream using #:: operator.

For example

We have created a Stream having three elements 1,2 and 3.

Since Stream is eagerly evaluated at the head. Therefore, head of the stream has been printed but the tail is lazily computed. That’s why it has not been computing yet. It will be computed on demand.  The toStream method can convert any collection to Stream.

For example,  find the first 10 primes after 100.

generatePrimes has an infinite number of primes and we are interested in getting 10 primes only greater than 100. When we apply the take method, it still does not give us the result. Because Streams does not evaluate until it is no longer needed.

How to get the value from a stream?

We could either use force method to get the value from Stream or we could use the toList method to get a list of primes.

 

View

A view is a special kind of collection that represents some base collection but implements all methods lazily.

For example,

Here, I have tried to create a list of million elements and taking first 20 odd numbers. OOPS! I got OOM error.  But with view,

This time there is no OOM error, the reason is stream has never allocated memory for all million elements. Memory is allocated only for needed elements.

The difference between Stream and view is lazy view is lazy in evaluating the methods whereas Stream is ultimately lazy. A stream has no value. It generates value only when we ask for it. So, it is possible values are already there in case of view.  Besides that, Streams caches the result but the view doesn’t. In a view, elements are recomputed each time they are accessed. In a stream, elements are retained as they are evaluated.

Let me summarise what we have talked so far, we talked about non-strictness as a fundamental technique for implementing efficient and modular functional programs. Stream and views are ways to achieve lazy evaluation in scala. It helps to avoid creating intermediate collections and makes code faster.

Please free to give suggestions and comment!

References:

  1. View
  2. Stream
  3. Functional Programming in Scala

knoldus-advt-sticker


Written by 

I am a Software Consultant with experience of more than 1.5 years. I am a Functional Programing i.e Scala and Big Data technology enthusiast.I am a active blogger, love to travel, explore and a foodie.

2 thoughts on “Getting Lazy With Scala

Leave a Reply

%d bloggers like this: