Scala – Pattern Matching & Collections

Pattern matching is not just limited to simple types it can also be applied to collections as well. Let’s see how it can be applied to Lists. See Below In the above example, we want to check if the list of 3 elements starts with 10 or not. This logic works fine when the list has just 3 elements. But fails if the list has more than 3 elements. The last print shows the failure condition.  Extending the logic so that lists of any size can be checked. See Below. In the above code, the only change we made was in the second line of the function and introduced _* – is a special instance of type ascription which tells the compiler to treat a single argument of a sequence type as a variable argument sequence. It is not as complicated as it sounds essentially it means you can pass multiple … Read more

Scala Collection – Operators

To be honest there are no operators in Scala :). Every method in scala is an operator! But for some simplicity let’s just call them collection operators. Below are some of the operators which are common to all scala collections. The examples are mostly done using Arrays but can be applied to collections as well. length operator distinct – More importantly it returns an array of elements. range, fill head, tail, last head operator returns the first element of the array. tail returns all the elements but the first element.last returns the last element of an array Let’s look at some more interesting ones filter is a transformer and returns a new collection. It requires a predicate function which is applied to every element in the collection and if it evaluates to true then that element is returned. filterNot is exactly the opposite and would return if the condition evaluates to false. In … Read more

Scala Collections – Streams, Maps & Sets

Streams Stream is similar to a List however, it can have unlimited elements. It is a Traversable but lazy in nature. A Stream in Scala consists of a head and tail which is lazily computed i.e elements are only evaluated when they are needed. Hence it can be inferred, laziness helps to make streams infinite. Transformer functions like map, filter etc are also applied lazily although care should be taken that not all functions are lazy – i.e max, sum etc. Elements can be added to a stream as below. Keep in mind that when you are adding an element to the stream we also need to append Stream.empty Additionally also checkout the #::: that is only required after the stream name and then it is #:: Transformer methods are lazily evaluated as show below – e.g filter Transformer methods are lazily evaluated as show below – e.g map Sets Set … Read more

Scala Collections – Lists, Sequences & Vectors

Below, I have provided some of the collections which are usually the most used. Starting from the regular ones like Sequences, Lists, Maps, Sets we go on to discuss some of the more interesting ones like Vectors, Streams. Lists List is probably the easiest to understand and hence most used data structure/collection. In scala a list is implemented as a class. I quote A class for immutable linked lists representing ordered collections of elements of type A By default, all lists are immutable. However the devil is in the detail. Check out the scala documentation here. Despite being an immutable collection, the implementation uses mutable state internally during construction. These state changes are invisible in single-threaded code but can lead to race conditions in some multi-threaded scenarios You can declare lists in various ways and they are documented below Lists are efficient when the programs which use them are only … Read more

Scala Collections – Introduction

Introduction I started looking at Scala collections after realising a need to polish some rough edges in the quest to become a better scala programmer. The scala collections series of blogs are my aim to become better at scala. It has been written from a beginner’s perspective but I hope I would be equally helpful to seasoned programmers. Scala collections are a vast a topic and there is something for everyone if you know where to start. There are few commonly used collections that are described here and is a good place to start. There are many collections which are not covered and hopefully this blog will give you an idea as to where go and look and how to implement them. Let’ start – Scala collections have a very well defined & documented hierarchy available here. However, I have taken the liberty to insert a few images from the … Read more