Tuesday, 29 August 2017

Reactive Programming

Reactive programming is an asynchronous programming paradigm concerned with data streams.
In Reactive programming we define data source and data consumer and establish a connection between source and consumer than library take care of data flow from source to consumer.

Using RxJava api we can define the source and consumer as you can see in below code.

import rx.Observable;
import rx.Subscriber;

public class Test {

            public static void main(String[] args) {
                        Observable<Integer> source = Observable.range(1, 10);
                        Subscriber<Integer> consumer = new Subscriber<Integer>() {
                                    public void onNext(Integer number) {
                                                System.out.println(number);
                                    }
                                    public void onError(Throwable e) {
                                                System.out.println("error " + e.getLocalizedMessage());
                                    }
                                    public void onCompleted() {
                                                System.out.println("completed");
                                    }
                        };
                        source.subscribe(consumer);
            }
}

Output of above code:-
1
2
3
4
5
6
7
8
9
10
completed


Please refer below link for detail understanding of reactive programming.

Reactive Stream JAVA 9-

Reactive stream is asynchronous processing of data stream.
When we discuss about data processing than there is at least one data producer and one data consumer, data flow from producer to consumer, with a data pipeline in real time , and there is a processor to transform the data.
Java 9 Reactive stream works on same concept. It have flow package, contain Publisher, Subscriber and Processor refer below-
java.util.concurrent.Flow.Publisher
java.util.concurrent.Flow.Subscriber
java.util.concurrent.Flow.Processor

Refer below code snippet of simple implementation of Stream API
package features.java9;

import java.util.concurrent.Flow.Publisher;
import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription;

public class TestDataFlow  {

      Publisher<Integer> publisher = new Publisher<Integer>() {

            public void subscribe(Subscriber<? super Integer> subscriber) {
                  int count = 0;
                  while(count < 10) {
                        subscriber.onNext(++count);
                  }
            }
      };
     
      static Subscriber<Integer> subscriber = new Subscriber<Integer>() {
           
            public void onSubscribe(Subscription subscription) {
            }
           
            public void onNext(Integer item) {
                  System.out.println(item);
            }
           
            public void onError(Throwable throwable) {
            }
           
            public void onComplete() {
            }
      };
     
      public static void main(String args[]) {
            new TestDataFlow().publisher.subscribe(subscriber);
      }

}

package features.java9;

import java.util.concurrent.Flow.Publisher;
import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription;

public class TestPublisher implements Publisher<Long> {

      private static int publisherCount = 0;
      public void subscribe(Subscriber<? super Long> subscriber) {
            while(publisherCount < 10) {
                  subscriber.onNext(System.nanoTime());
            }
            subscriber.onComplete();
      }

      public static void main(String[] args) {
            new TestPublisher().subscribe(new Subscriber<>() {
                  public void onSubscribe(Subscription subscription) {
                  }
                  public void onNext(Long item) {
                        try {
                              Thread.sleep(1000);
                              System.out.println("randome value = " + item);
                        } catch (InterruptedException e) {
                        }
                        publisherCount++;
                  }
                  public void onError(Throwable throwable) {
                        System.out.println("error");
                  }
                  public void onComplete() {
                        System.out.println("done");
                  }
            });
      }
}



Monday, 28 August 2017

Functional Programming


Hello Friends, Let’s discuss about Functional Programming.

What is Functional Programming?
Functional programming is nothing just mathematical functional which perform some operations. Functional programming helps to write compact and concise code. It’s provide a very high level of abstraction although it’s reduces the code readability.

Functional programming contains the following key concept.
1.      Function as first class object
2.      Pure function.

Function as first class object
Function as first class object means we can create the instance of function similar to String, Map, List etc. Function can be passed as parameter to other functions.

Pure function
1.      A pure function have no state.
2.      It’s immutable
3.      Its return value is dependent on input variable only.
4.      Pure function is a pure mathematical expression.

Higher order functions are function which contain another function in parameter or return value is a function.

Here we are adding few example of function.

Example:

import java.util.function.BiFunction;
import java.util.function.Function;

public class Test {

      public static void main(String[] args) {
            Function<Integer,Integer> addFunc = x -> x + 1;
            System.out.println(addFunc.apply(2));
           
            BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
            System.out.println(add.apply(2, 3).intValue());
           
            BiFunction<Integer, Integer, Integer> substract = (a, b) -> a - b;
            System.out.println(substract.apply(2, 3).intValue());
           
            BiFunction<Integer, Integer, Integer> multiply = (a, b) -> a * b;
            System.out.println(multiply.apply(2, 3).intValue());
           
            BiFunction<Integer, Integer, Integer> devide = (a, b) -> a / b;
            System.out.println(devide.apply(2, 3).intValue());
           
            Function<Integer, Function<Integer,Integer>> adder = x -> y -> x + y;
            System.out.println(adder.apply(1).apply(1));
      }
}

Output:-
3
5
-1
6
0
2