Scalafmt – Styling The Beast

Knoldus

“I have never seen elegance go out of style” – SONYA TECLAI

Scala (The Beast) has imported efficiency and elegance to the programming world by combining coziness of object oriented programming with the magic of functional programming. This blog illuminates the work of Ólafur Páll Geirsson who has done a great job of styling the beast. In the bulk below I will discuss my experience of using Scalafmt, its installation process and some of its cool code styling features. Stuff we would be using in this blog

  • Editor -> Intellij (v 2017)
  • Build Tool -> sbt (v 0.13.15)
  • Language -> Scala (v 2.12.1)

One of the most important aspect of good code is its readability which comes with good and standard formatting. Ever wondered how an entire project having around 1000 Scala files of poorly formatted code could be formatted without having a headache? Well going shft + ctrl + alt…

View original post 634 more words

Advertisements

How to lazily evaluate collections in Scala

Knoldus

The Scala language provides to ways to implement the collections. One is strict and other is non-strict or lazy.

Whenever, the instance of a collection (except for the Streams) is created, it creates the strict version of the collection which means memory is allocated at the same time.

A simple example of strict collection is:


val list = List(1,2,3,4,5,6,7,8,9,10)
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

The above code, the memory would be allocated for the List immediately.

But whenever a view in the collection is created, it makes that collection as a lazy collection which means memory allocation would not be done at the time of initialization, instead, it would take place whenever they are actually accessed or some transformations are applied on them.

For example,


val listView = List(1,2,3,4,5,6,7,8,9,10).view
listView: scala.collection.SeqView[Int,List[Int]] = SeqView(...)

Here, the type of listView is SeqView[Int, List[Int]]

View original post 389 more words