Void NullPointerException with Scala Option
May 22, 2017 · 3 min readIf you tell me that in your programming career, you have never been inflicted at-least once with a NullPointerException, you were simply the best programmer out there. During the early days of my career. I can remember times and times again, I ended up hitting NullPointerException's until I really made it a point to …
Read MoreTest private methods with Scala Test
Apr 26, 2017 · 2 min readI will not debate upon whether writing private methods are a code smell or not but rather just show how a private method can be unit tested using the Scala Test framework Here is our test class where we would write a unit test which tests a private method called myPrivateMethod in a class called MyClass 1class …
Read MoreScala's Dynamic Proxy
Mar 23, 2017 · 2 min readOne of the coolest features since Scala 2.10.3 are the Scala dynamic types. Let's look at what they are! A dynamic type is a type with which we can dynamically add fields / methods to an existing type. This is better explained with some examples Assume that we have a scala class as defined below: 1 class MyClass { 2 …
Read MoreConverting collections to and fro in Scala / Java
Mar 14, 2017 · 4 min readSay that you have to use a Java library in your Scala application and this Java library has a couple of API's that require you to pass a Java collection type. Let's see how we can cope with it by looking at the following examples: Suppose I have a Java ArrayList that contains some Integers that I want to map these List …
Read MoreImplicit Classes Scala 2.10 Extension Methods
Jan 16, 2017 · 2 min readHaving worked with C# in the last couple of months, writing extension methods was one of the cool features that I appreciated a lot! Extension methods are just a way to add new methods to the existing set of types. For example, the double type has several methods as part of the Scala API. If you now want to add a …
Read MorePattern Matching Scala Collections
Jan 4, 2017 · 2 min readOne of the astonishing features that Scala has is the pattern matching mechanism. Think of it like a Java Switch statement on Steroids. Pattern matching is such a powerful concept in Scala. Once you start using them, you'll find it inevitable to not using them. Let's look at some examples You want to pattern match a …
Read MoreMap'em up
Dec 27, 2016 · 2 min readNo, it is not the Map implementation that we are going to talk about. Let me ask you a very simple question. I assume that you have written functions or methods or routines or whatever you call it. What do you think that it actually does? ...., 3...., 2...., 1.... TimeUp. Let me answer that for you. A function or a …
Read MoreWhat the heck are Scala's ExecutionContext's?
Dec 27, 2016 · 3 min readAn ExecutionContext or otherwise called a thread pool is vital when running asynchronous code. In Scala, there is a global ExecutionContext that you can supply to your asynchronous code blocks. An ExecutionContext is just an Executor of a Task. It executes a Task in such a way that it is free to execute the Task in a …
Read MorePartial Functions in Scala
Sep 23, 2016 · 4 min readBefore taking a look at what partial functions in Scala is, let's look at some examples that map an assorted List of elements: 1 val assorted = List(1,2,"joe","san") 2 assorted map { case i: Int => i + 1 } The code above when tried results in a match error. The reason being that we have an anonymous …
Read MoreMaking a Case for Scala's case classes
Aug 25, 2016 · 3 min readIf you are like me that is fed-up with writing or generating those getters / setters in your domain objects or Data Transfer Objects or Value Objects, deferring the process of writing at compile time is definitely a boon. Scala's case classes does exactly that. Some noteworthy things to know about Scala case classes. …
Read More