Simplifying Monads in Scala

Monads are not at all a complex topic, but yes it comes under the advanced section of Scala language. So basically Monads is a structure that represents sequential computations.

Monads is a structure that represents sequential computations, Monad is not a class or a trait; monad is a concept.

A monad is an object that wraps another object in Scala, In Monad the output of a calculation at any step is the input to the other calculation which runs as a parent to the current step.

One positive point: you already know about flatMap in Scala and you know how to access the future value using combinators and that consists flatMap, this way you already know what is the use case of flatMap., it flattens the resulting list of strings into a sequence of characters, see below example:


scala> var name = Seq("Nikhil", "Mateo")
name: Seq[String] = List(Nikhil, Mateo)

scala> var exMap =
exMap: Seq[String] = List(nikhil, mateo)

scala> var exMap = name.flatMap(_.toLowerCase)
exMap: Seq[Char] = List(n, i, k, h, i, l, m, a, t, e, o)

Continue reading

Posted in Scala | Tagged , , | 1 Comment

Lambda Expression.

As a Java developer, it becomes your need to study about the new concepts and APIs keeps popping in and learn them to apply it in your development and compete the world. One of the biggest changes in Java 8 is Lambda Expressions. Nowadays several JVM based Programming Languages like Groovy, Ruby and Scala etc. keeps talking about how much functional they are and how much concised code they write, just keeps praising about the features. And hence come the Java feature which helps making more functional Applications in Java and much more than that i.e. Lambda Expressions.

1. What is Lambda Expression?

Lambda is a new programming construct, which lets you write Java applications in a completely different paradigm from the traditional Obejct Oriented Programming. I will be giving a quick overview of what is Lambda? Why should we use it? How to use it? Lambdas under the hood and how Lambda has simplyfied Java APIs and their use.

A lambda expression is an annonymous function. Whenever a single abstracted method Interface is expected or we need to have a function which should be loaded dynamically into the memory we can go for Lambda. There may be many other more correct definitions to it but this is what I understood after studing Lambda. Take a look at the fuction below:

public class DemoLambda {

	public static void main(String... a) {
		Thread thread = new Thread(new Runnable() {
			public void run() {
				System.out.println(a[0] + " : from New Thread");
		System.out.println(a[0] + " : from main Thread");

$java DemoLambda “Hello Lambda!!!”
Hello Lambdas!!! : from main Thread
Hello Lambdas!!! : from New Thread

Continue reading

Posted in Java | Leave a comment

Getting started with Ethereum


What is Ethereum?

Ethereum is an open-source blockchain platform that lets anyone build and use decentralized applications that run on blockchain technology. It is an open-source project created by Vitalik Buterin.

How is it different from Bitcoin?

Bitcoin is not capable of performing calculations that didn’t fit into his limited scripting language.

Satoshi Nakamoto, the creator of bitcoin, did not design it to send anything more than few kilobytes of data per transaction believing that limiting the functionality would improve the security of the system.

But unlike Bitcoin, Ethereum was designed as the one that fits a virtual machine (the EVM), a Turing-complete language(Solidity), a token (ETH), and fuel (gas) to power every transaction in its network. This combination allows to create complex programmatic computational instructions or as we call them smart contracts and decentralized applications or DApps.

How does it work?

The basic unit of Ethereum is account. Accounts are essential for users to interact with the Ethereum blockchain via transactions.The ethereum blockchain tracks the state of every account.

There are two types of accounts:

  • externally owned accounts(EOAs) – which are controlled by private keys
  • contract accountswhich are controlled by their contract code and can only be activated by an EOA

If we restrict Ethereum to only externally owned accounts and allow only transactions between them, we get an “altcoin” system that is less powerful than bitcoin itself and can only be used to transfer ether.

Like in Bitcoin, users pay small transaction fees to the Ethereum network. This helps in protecting  the Ethereum blockchain from malicious computational tasks, like DDoS attacks or infinite loops. These fees are paid in amounts of Ethereum’s native value-token, Ether.

Continue reading

Posted in Blockchain, Scala | Tagged , , , , , , | 1 Comment

Scala+Gradle+Cucumber = A holy alliance

It has been some long time that I have not written a blog regarding my new technological learnings. And that is the void I will be wanting to fill in the coming days. This one being the start.

So, I have been working on work items that include Scala, Gradle and Cucumber together. That’s right. Together. And with all that I was able to learn/read over the internet, their togetherness has proved to be a bumpy ride for many. Well, initially, for me too. But as time passed and my interaction with these three beauties increased, I realized it’s not that bad too. In this blog, I am trying to demonstrate a simple setup of these three together. In future, I may want to expand on the complexity. But today it will be simple and useful. Continue reading

Posted in Scala | Leave a comment

How to cope with the threat of System failures?


Systems are designed to respond to its users in any case. But, some of the times, due to some reasons system may fail. System failures can cause huge loss to its users as well as also creates some sort of fear into them; and with this fear, users lose trust in the system which definitely affects system’s goodwill and market value. For example, a few months back, Amazon s3 services were unavailable for some hours, caused an outage to lots of popular websites including Quora (it a popular question-answer portal), which affected a lot of its users. No doubt that Quora was innocent in this case because it was totally dependent on s3, but its users hardly care about all these. They only found that Quora was down for some hours. This is one of the recent examples of System failure, there are definitely a lot more on this.


System Failure can be due to following reasons:

  • A machine on which your application is running may fail.
  • Network failure due to which your application gets completely disconnected from the network.
  • Some manual edits by Humans and they accidentally make the wrong edits or updates.
  • Data centers put their entire system on maintenance.
  • And lot more reasons…

The question arises that how to handle these sort of situations?

Continue reading

Posted in Scala | Leave a comment

Integrating Unmanaged Service in Lagom with Scala

So, you want to integrate an unmanaged service in Lagom using Scala.
Let’s first understand what an unmanaged service first and then we will see How we can integrate an unmanaged service with our lagom service.

What is an unmanaged service?

An unmanaged service is basically an exernal service which exposes some data over HTTP. Any external REST API which will be used in Lagom based service is an unmanaged service.

Any external REST API which will be used in Lagom based service is an unmanaged service.
For example : Integration of GoogleMap, Openweather and any other for that matter.

Communicating with external(unmanaged) services in Lagom

  • It is possible to enable communication between the Lagom services defined in your build, and an unbounded number of external services (which could either be running locally or on a different machine).
  • To do this, we need to register the API as external service in the Service Locator, which allows services to discover and communicate with each other.
  • The first thing you will have to do is to register each external service in the Service Locator. Assume we want to register an external service named weather that is running on http://localhost:3333, here is what we would add to the build.
    lagomUnmanagedServices in ThisBuild := Map(“weather” -> “http://localhost:3333”)
    where http://localhost is the hostname and 3333 is port number
  • The above ensures that the Service Locator knows about the weather service. Then, if you need a Lagom service to communicate with it, simply @Inject the ServiceLocator and use it to either locate the weather service’s URI, or perform some arbitrary work with it.

Now Let’s see How  we can consume data from the unmanaged service.

Consume data from unmanaged service

Step 1. Create a new ‘trait’ for the external service which will have exact same route as external service has. Continue reading

Posted in Scala | 1 Comment

Introduction to D3.js {Part -3}

The charts are more meaningful when they have axes around them. The axes tell the quantity of a particular datum represented on the plane. This a how-to-post to create the axes around a D3 chart.

D3 provides .json() method to load the JSON data from a URL. This method takes two parameters – first, a URL that returns a JSON data and second, an anonymous method that takes action on the data coming from the server (current directory in my case). This anonymous method is asynchronous by nature and and takes at most two parameter. The following lines loads the data from a JSON file from my current directory:

	var svgHeight = 500;
	var svgWidth = 800;
	var paddingH = 50;
	var paddingV = 50;
	var dataset = undefined;
	var maxX, xScale, yScale, radiusScale, colorXScale, colorYScale;
	/*----- Working with the D3 rendering -----*/
	var svg ='body')
			.attr('width', svgWidth)
			.attr('height', svgHeight);
	d3.json('points.json', function ( error, data ) {
		if(error == null || error == undefined) {
			dataset = data;
		} else {
			window.alert('Something wrong happened while loading the data from JSON file. Try again.');

Continue reading

Posted in Scala, D3 Charts | Tagged , , , , | Leave a comment

Implementing Decision Tree Using Smile-Scala

Smile is a machine learning library based on JAVA and provides Scala API too although most of the classes it uses are implemented in JAVA. To know more about smile please go through this introductory blog.

Here in this blog, we will implement decision tree using Smile library. To know more about the decision trees you can check out this blog


To use smile the following dependency we need to include in our sbt project: Continue reading

Posted in machine learning, Scala | Tagged , , | 2 Comments

How data is handled between Components in React (State vs Props)

Prerequisites :

Basic understanding of React. Here’s a blog you can refer to :
Getting Started with React


Every component in react have a property called props associated to it. This lets you make a single component that is used in many different places in your app, with slightly different properties in each place.

If we write a react component in es5, it’s nothing but a function that takes props as parameters and returns JSX element.

A parent component can send some data to its child components via props.

But HOW?

We’ll find that out in a minute, but first there are two rules that we need to cover.

  1. RULE 1 : Props are read only
    That means, you cannot re-assign to the props.
  2. RULE 2 : Every component must be a pure function with respect to its props.
    A pure function is a function whose output is always same for the same arguments. For eg :
    sum(a, b) {
       return a + b;

Continue reading

Posted in JavaScript, React, Reactive, ReactJS | Tagged , , , , , | 1 Comment

Getting Started with React

React (sometimes called React.js or ReactJS) is an open-source JavaScript library for building user interfaces.

React allows developers to create large web-applications that use data that can change over time, without reloading the page.

It aims primarily to provide speed, simplicity and scalability.

React is flexible. You can create a separate project on react, or you can add it to an existing code base of any framework like Angular.


First thing you need to know for React is JSX. JSX is a syntax extension of Javascript and it looks like :

It may look like a template language, but it comes with the full power of JavaScript, like you can embed expressions in JSX as well. For eg:

Continue reading

Posted in JavaScript, React, ReactJS | Tagged , , , | 1 Comment