XenonStack

A Stack Innovator

Post Top Ad

Showing posts with label functional programming. Show all posts
Showing posts with label functional programming. Show all posts

Wednesday, 7 June 2017

6/07/2017 11:53:00 am

Overview of Kotlin & Comparison With Java

 

What is Kotlin Language?


Kotlin is a new programming language from JetBrains. It first appeared in 2011 when JetBrains unveiled their project named “Kotlin”. Kotlin is an Open-Source Language.

Basically like Java, C and C++ - Kotlin is also “statically typed programming language”. Statically typed programming languages are those languages in which variables need not be defined before they are used. This means that static typing has to do with the explicit declaration or initialization of variables before they are employed.

As Earlier said that Java is an example of a statically typed language, similarly C and C++ are also statically typed languages.

Basically, Static typing does not mean that we have to declare all the variables first before we use them. Variables may be initialized anywhere in the program and we (developers) have to do so, to use those variables anywhere in the program when there is a need. Consider the following example -
/* Java Code */
static int num1, num2; //explicit declaration
num1 = 20; //use the variables anywhere
num2 = 30;

/* Kotlin Code*/
val a: Int
val b: Int
a=5
b=10
 
In addition to the classes and methods of object-oriented programming, Kotlin also supports procedural programming with the use of functions.

Like in Java, C and C++, the entry point to a Kotlin program is a function named “main”. Basically, it passed an array containing any command line arguments. Consider the following example -

/* Kotlin Code*/
/* Simple Hello Word Example*/

//optional package header
package hello 

//package level function, which return Unit and takes an array of string as parameter
fun main(args: Array < String > ) { 
 val scope = “world”
 println(“Hello, $scope!”) //semicolons are optional, have you noticed that? :)
}
 
Filename extensions of the Java are .java, .class, .jar but on the other hand filename extensions of the Kotlin are .kt and .kts.
 
 
  

Today (May 17, 2017), at the Google I/O keynote, the Android team announced that Kotlin will be the Official Language of Android. For More Info, Visit the Link.

Benefits of Kotlin Language


  • Kotlin compiles to JVM bytecode or JavaScript - Like Java, Bytecode is the compiled format for Kotlin programs also. Bytecode means Programming code that, once compiled, is run through a virtual machine instead of the computer’s processor. By using this approach, source code can be run on any platform once it has been compiled and run through the virtual machine. Once a kotlin program has been converted to bytecode, it can be transferred across a network and executed by JVM(Java Virtual Machine).

  • Kotlin programs can use all existing Java Frameworks and Libraries - Yes, it's true that Kotlin programs can use all existing java frameworks and libraries, even advanced frameworks that rely on annotation processing. The main important thing about kotlin language is that it can easily integrate with Maven, Gradle and other build systems.

  • Kotlin can be learned easily and it is approachable. It can be learned easily by simply reading the language reference.The syntax is clean and intuitive(easy to use and understand). Kotlin looks a lot like Scala but is simpler.

  • Kotlin is Open Source and it costs nothing to adopt.

  • Automatic conversion of Java to Kotlin - JetBrains integrated a new feature into IntelliJ which converts Java to Kotlin and saves a huge amount of time. And it also saves us to retype mundane code.

  • Kotlin’s null-safety is great - Now get rid of NullPointerExceptions. This type of system helps us to avoid null pointer exceptions. In Kotlin the system simply refuses to compile code that tries to assign or return null. Consider the following example -

val  name: String = null  // tries to assign null, won’t  compile.
 
fun  getName() : String = null  // tries to return null, won’t  compile.
 
 
 
 
  • Code reviews are not a problem - Kotlin is much more focuses on readable syntax so code reviews are not a problem, they can still be done by those team members who are not familiar with the language.

Features of Kotlin Language


  • The billion dollar mistake made right. As already mentioned above that Kotlin avoids the null pointer exception. If we try to assign or return null to a variable or function respectively, then it won’t compile.

But in some special cases if we need nullability in our program then we have to ask Kotlin very nicely. Every Nullable type require some special care and treatment. We can’t treat them the same way         as non-nullable types and this is a very good thing.

We have to add “?” after the variable type. Consider the following example - Kotlin also fails at compile-time whenever a NullPointerException may be thrown at run-time. Consider the following example -
val name: String? = null    //assigned  null and it will compile  also.
 
fun getName() : String? = null //returned null and it  will compile too.
 
 
/* won’t compile */ 
  val name: String? = null
  val len = name.length 

/* correct way */ 
  val name: String? = null 
  val len = name?.length
 

  • Versatile


 

  • Lean Syntax and Concise - One liner functions take one line, simple structs/JavaBeans can also be declared in one line. Real properties generate getters and setters behind the scenes for Java interop. And Adding the data annotation to a class triggers autogeneration of boilerplate like equals, hashCode, toString and much more.

Consider the following example -

/*  Java program */ 

public class Address {
  
   private String street;

   private int streetNumber;

   private String postCode;

   private String city;

   private Country country;

   public Address(String street, int streetNumber, String postCode, String city, Country country) {

       this.street = street;

       this.streetNumber = streetNumber;

       this.postCode = postCode;

       this.city = city;

       this.country = country;

   }

   @Override
   public boolean equals(Object o) {
       if (this == o) return true;

       if (o == null || getClass() != o.getClass()) return false;

       Address address = (Address) o;

       if (streetNumber != address.streetNumber) return false;

       if (!street.equals(address.street)) return false;

       if (!postCode.equals(address.postCode)) return false;

       if (!city.equals(address.city)) return false;

       return country == address.country;
   }

   @Override
   public int hashCode() {
       int result = street.hashCode();

       result = 31 * result + streetNumber;

       result = 31 * result + postCode.hashCode();

       result = 31 * result + city.hashCode();

       result = 31 * result + (country != null ? country.hashCode() : 0);

       return result;

   }

   @Override

   public String toString() {

       return "Address{" +

               "street='" + street + '\'' +

               ",     streetNumber=" + streetNumber +

               ",     postCode='" + postCode + '\'' +

               ",     city='" + city + '\'' +

               ",     country=" + country +

               '}';

   }

   public String getStreet() {

       return street;

   }

   public void setStreet(String street) {

       this.street = street;

   }

   public int getStreetNumber() {

       return streetNumber;

   }

   public void setStreetNumber(int streetNumber) {

       this.streetNumber = streetNumber;

   }

   public String getPostCode() {

       return postCode;

   }

   public void setPostCode(String postCode) {

       this.postCode = postCode;

   }

   public String getCity() {

       return city;

   }

   public void setCity(String city) {

       this.city = city;

   }

   public Country getCountry() {

       return country;

   }

   public void setCountry(Country country) {

       this.country = country;

   }

}
 
 
/*  Kotlin Program */

data class Address(var street:String,

       var streetNumber:Int,

       var postCode:String,

       var city:String,

       var country:Country)
 
 


Difference Between Kotlin And Java

 


  • Null Safety - As already mentioned in above section that Kotlin avoids NullPointerException. Kotlin fails at compile-time whenever a NullPointerException may be thrown.

  • Data Classes - In Kotlin there are Data Classes which leads to autogeneration of boilerplate like equals, hashCode, toString, getters/setters and much more...

    Continue Reading About Kotlin V/s Java At: XenonStack.com/Blog

 

 

 

 

Wednesday, 26 April 2017

4/26/2017 10:43:00 am

Enabling Real Time Analytics For IoT



What is Fast Data?


A few years ago, we remember the time when it was just impossible to analyze petabytes of data. Then emergence of Hadoop made it possible to run analytical queries on our huge amount of historical data.

As we know Big Data is a buzz from last few years, but Modern Data Pipelines are constantly receiving data at a high ingestion rate. So this constant flow of data at high velocity is termed as Fast Data.

So Fast data is not about just volume of data like Data Warehouses in which data is measured in GigaBytes, TeraBytes or PetaBytes.

Instead, we measure volume but with respect to its incoming rate like MB per second, GB per hour, TB per day. So Volume and Velocity both are considered while talking about Fast Data.

What is Streaming and Real-Time Data


Nowadays, there are a lot of Data Processing platforms available to process data from our ingestion platforms. Some support streaming of data and other supports true streaming of data which is also called Real-Time data.

Streaming means when we are able to process the data at the instant as it arrives and then processing and analyzing it at ingestion time. But in streaming, we can consider some amount of delay in streaming data from ingestion layer.

But Real-time data needs to have tight deadlines in the terms of time. So we normally consider that if our platform is able to capture any event within 1 ms, then we call it as real-time data or true streaming.

But When we talk about taking business decisions, detecting frauds and analyzing real-time logs and predicting errors in real-time, all these scenarios comes to streaming. So Data received instantly as it arrives is termed as Real-time data.
 

Stream & Real Time Processing Frameworks


So in the market, there are a lot of open sources technologies available like Apache Kafka in which we can ingest data at millions of messages per sec. Also Analyzing Constant Streams of data is also made possible by Apache Spark Streaming, Apache Flink, Apache Storm.


Spark Streaming



















Apache Spark Streaming is the tool in which we specify the time-based window to stream data from our message queue. So it does not process every message individually. 

We can call it as the processing of real streams in micro batches.
Whereas Apache Storm and Flink have the ability to stream data in real-time.

Why Real-Time Streaming


As we know that Hadoop, S3 and other distributed file systems are supporting data processing in huge volumes and also we are able to query them using their different frameworks like Hive which uses MapReduce as their execution engine.

Why we Need Real-Time  Streaming?


A lot of organizations are trying to collect as much data as they can regarding their products, services or even their organizational activities like tracking employees activities through various methods used like log tracking, taking screenshots at regular intervals.

So Data Engineering allows us to convert this data into structural formats and Data Analysts then turn this data into useful results which can help the organization to improve their customer experiences and also boost their employee's productivity.

But when we talk about log analytics, fraud detection or real-time analytics, this is not the way we want our data to be processed.The actual value data is in processing or acting upon it at the instant it receives.

Imagine we have a data warehouse like hive having petabytes of data in it. But it allows us to just analyze our historical data and predict future.

So processing of huge volumes of data is not enough. We need to process them in real-time so that any organization can take business decisions immediately whenever any important event occurs. This is required in Intelligence and surveillance systems, fraud detection etc.

Earlier handling of these constant streams of data at high ingestion rate is managed by firstly storing the data and then running analytics on it.

But organizations are looking for the platforms where they can look into business insights in real-time and act upon them in real-time.

Alerting platforms are also built on the top of these real-time streams. But Effectiveness of these platform lies in the fact that how truly we are processing the data in real-time.

Use Of Reactive Programming & Functional Programming


Now when we are thinking of building our alerting platforms, anomaly detection engines etc on the top of our real-time data, it is very important to consider the style of programming you are following.

Nowadays, Reactive Programming and Functional Programming are at their boom.

So, we can consider Reactive Programming as subscriber and publisher pattern. Often, we see the column on almost every website where we can subscribe to their newsletter and whenever the newsletter is posted by the publisher, whosoever have got subscription will get the newsletter via email or some other way.

So the difference between Reactive and Traditional Programming is that the data is available to the subscriber as soon as it receives. And it is made possible by using Reactive Programming model.

In Reactive Programming, whenever any events occur, there are certain components (classes) that had registered to that event. So instead of invoking target components by event generator, all targets automatically get triggered whenever any event occurs.

Now when we are processing data at high rate, concurrency is the point of concern. So the performance of our analytics job highly depends upon memory allocation/deallocation. So in Functional Programming, we don’t need to initialize loops/iterators on our own.

We will be using Functional Programming styles to iterate over the data in which CPU itself takes care of allocation and deallocation of data and also makes the best use of memory which results in better concurrency or parallelism.

Streaming Architecture Matters


While Streaming and Analyzing the real-time data, there are chances that some messages can be missed or in short, the problem is how we can handle data errors.

So, there are two types of architectures which are used while building real-time pipelines.
  • Lambda Architecture:

    This architecture was introduced by Nathan Marz in which we have three layers to provide real-time streaming and compensate any data error occurs if any. The three layers are Batch Layer, Speed layer, and Serving Layer.
    lambda architecture







  





Continue Reading the full Article At - XenonStack.com/Blog