Showing posts from December, 2017

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") …

Creating Actor in Akka

In Scala, we can create an Actor by extending the class and writing the receive method in that class. Normally we will write the receive method by using the case statement that will allow the actor to respond t the different type of the messages it receives.

lets first create a project using the SBT name HelloAkka. Let's define our necessary resolver and dependency resolver information in the build.sbt file which looks like this.

name := "HelloAkka"

version := "1.0"

scalaVersion := "2.12.1"

resolvers += "Typesage Repository" at

libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.4.12"

if you are new to sbt don't worry it just like a maven or gradle which is esp build for scala and with much more features

Now lets write our first HelloAkka class{Actor, ActorSystem, Props} /** * Created by yubrajpokharel on 1…

Actor's in Akka Framework

As in Java, we can compare Actor as a thread. But in comparison to the threads, Actors are much lighter. Its a much higher level of abstraction than the threads. The implementation is also much simpler than as compared to the threads. Once we grab the main idea of this model it let us focus on our requirements more than managing the low-level problems like a lock and shared data as in threads.

Some of the main features of Akka actor library are

It is fault tolerance which means it can be used in creating the "self-healing systems".As per the documentation, there can be 2.7 million actors per GB of RAM.It provides us location transparency. It can span multiple JVM's and servers it is designed to work as distributed servers.Alvin Alexander in his book called Scala Cookbook states that 
"I like to think of an actor as being like a web service on someone else's servers that I cant control. I can send messages to that web service to ask if to do something, or I can quer…

What is Functional Programming?

I call it my billion-dollar mistake … My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. — Tony Hoare

 Functional programming is sometimes considered to be a set of techniques that supplement or replace techniques found in other programming paradigms, such as

 First-class functions
 Anonymous functions
 Closures
 Currying
 Lazy evaluation
 Parametric polymorphism
 Algebraic data types
we can consider the imperative approach of programming as the opposite of functional programming. An imperative style is composed of lots of elements that actually do something which depends on the initial state, transition state, and the end state, this mecha…