What is Transpiling?

Although we write our code in ES6, some of the browsers may not support it or may not support the whole ES6 code. And the only way to ensure that your ES6 code work is to convert it into the ES5 code, and this process is called transpiling. And one the most famous tool for this process is Babel.

Let us say we are writing a code in ES6 format just to calculate the mean of three numbers

const getMean = (x=1, y=2, z=3) => { var mean = (x + y + z) /3; return mean; }
after running this code in the transpiler we will get

"use strict"; var getMean = function getMean() { var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; var z = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 3; var mean = (x + y + z) / 3; return mean; };

it uses "add strict" declaration to run this script in the script mode and all the v…

sealed keyword in Scala

sealed classes or traits can only be extended within the same file where it is defined. This is especially useful while creating the Algebraic Data Types.

Here are some of the characteristics of sealed traits

sealed traits should be extended within the same file so that the compiler knows all the possible subtypes otherwise the compiler can emit warnings if a match/case expression is not exhaustiveit cannot be extended anywhere else the same fileshould only be used if all the subtypes are early known in advance.this can be a way of creating something like an enum in Java

we can see the use of sealed traits in scala's Option like

sealedtraitOption[+A] finalcaseclassSome[+A] extendsOption[A] objectNoneextendsOption[Nothing]
so if we use these traits with pattern matching like this then we will get a compile error saying
Warning: match may not be exhaustive. It would fail on the following input: None

opt match { caseSome(a) =>"hello" }
and another benefit of using thi…

object keyword in scala

The object keyword in scala creates a new singleton type, similar to the singleton class in Java i.e. its instance cannot be created twice and only has a single named instance. It's just like creating a new instance of the anonymous class in Java. Scala doesn't have a static concept. So scala creates a singleton object to provide an entry point for your program execution. If you don't create a singleton object, your code will compile successfully but will not produce any output. Methods declared inside Singleton Object are accessible globally. A singleton object can extend classes and traits.

objectSingleton{ def main(args:Array[String]){ //no object creation is needed here SingletonObject.hello() } objectSingletonObject{ def hello(){ println("Hello, This is Singleton Object") } }
Hello, This is Singleton Object

In scala, when we have a class with the same name as a singleton object, it is called as a compani…

Akka Actors in Java - Part 1

Whenever we are dealing with the large and enterprise applications we always need to focus more on synchronizations and locks. This stuffs are more complex and also is time-consuming. But with Akka, we can easily write asynchronous code without the need for locks and synchronization. Akka is based on the message-driven terminology so that we don't need to wait for a return value instead the receiver will reply the sender with a message that will contain the result. And all actors are monitored by the parents so that it will also be easier to notify and stop the actors if the failure happens. In order to use Actors in our system lets first add akka-actor dependency in our pom.xml

//As we discussed all the actors will be in the hierarchy so lets create first the actor sys…

Functional Programming in Java - Part 1

Let us Create a simple Class which validates the email. But we will start from creating in the classical java way.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34package advance.emailValidation; importjava.util.regex.Pattern; /** * Created by ypokhrel on 12/27/2017. */publicclassEmailValidator { privatestaticfinal String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; finalstatic Pattern emailPattern = Pattern.compile(EMAIL_PATTERN); staticvoidtestMail(String email){ if(emailPattern.matcher(email).matches()){ sendVerification(email); }else{ logError(email); } } staticvoidsendVerification(String s){ System.out.println("Email verified and send to : " + s); } privatestaticvoidlogError(String s){ System.out.println(…

Stopping the Actor in Akka

What if we want to stop the actor's in Akka. There are various ways to stop the Actor which we will discuss in this tutorial. But the basic ways are calling the actorSystem.stop(anActor) and context.stop(childActor).
We will use the second one if we need to stop the actor from inside the another actor. An actor can also stop itself by calling the context.stop(self)


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23{Actor,ActorSystem,Props}/** * Created by yubrajpokharel on 12/13/17. */objectstopMessage//extends App{val system =ActorSystem("normalStop")val actor = system.actorOf(Props[MyActor], name ="myactor") actor !Message system.stop(actor) system.terminate()}caseobjectMessageclassMyActorextendsActor{def receive ={caseMessage=> println(s"Actor get a call to act")case_=> println("Opps wrong number")}}
The other ways to stop actors are PoisionPill or gracefulStop.
let us dis…

Communication between two Actor's In scala

In order to understand the commutation between two actors lets take an example of pingpong app which uses two actors name Ping and Pong.** * Created by yubrajpokharel on 12/11/17. */caseobjectPingMessagecaseobjectPongMessagecaseobjectStartMessagecaseobjectStopMessageclassPing(pong:ActorRef) extendsActor{ var count =0def incrementAndPrint {count+=1; println("ping")} overridedef receive = { caseStartMessage=> incrementAndPrint pong ! PingMessagecasePongMessage=> incrementAndPrint if(count > 90) { sender ! StopMessage println("ping stopped") context.stop(self) }else{ sender ! PingMessage } case_=> println("ping got something unexpected") } } classPongextendsActor{ overridedef receive = { casePingMessage=> println(" pong") …