Converting 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 MoreScala's for expression
Jun 14, 2016 · 3 min readThis blog article takes a deeper look into Scala's for comprehensions. The for comprehension / expression in Scala has the following syntax: 1 for ( seq ) yield expr In deed a very simple syntax underneath which lies a very strong fundamental concept. The seq in the syntax above could be a sequence of generators, …
Read MoreAccessing Tuples in Scala
May 13, 2016 · 1 min readScala seems to be like a gold mine, the more you dig into, the more you get. While you deal with tuples, when you want to access the elements of a tuple, you have to use the default numbering given to you by the Scala compiler. Say for example., you have a tuple as defined below: 1val myTuple = (31, "Joesan", …
Read MoreSealed Traits in Scala
Apr 6, 2016 · 2 min readYou probably know the idea behind using case classes and pattern matching them. Sometimes you might want the help of the compiler to tell you if you have pattern matched correctly by covering all possible cases. You can now get this sort of compiler support by creating a superclass for the case classes and marking them …
Read More