Thursday, January 26, 2017

Concepts in Lamda programming

Usage of Function, Method Reference and Closure in functional programming

package com.java8.chapter3;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.ToIntFunction;

/* Example of Function<R,T> interface i.e. Converting an object from one DT to another DT
 * Use of Closure in lamda and anonymous inner class
 * Use of Method Reference, replacement of lamda
 * R apply(T t);
*/
public class FunctionLamda {
     public static void main(String[] args) {

List<Integer> list = Arrays.asList(2, 4, 6, 8);
          
// Printing list<Integer>
list.stream().forEach(p -> System.out.println(p));
          
// perform any operation on each element of list and print
System.out.println(performOperation(list,p->p*2));
          
// Create new Integer list(length of String) from list of Strings using  Function<String,Integer>

System.out.println(extractOperation(Arrays.asList("Paras","Suriender","Sham"), s->s.length()));
          
// Create new Integer list(length of String) from list of Strings using ToIntFunction<String>
System.out.println(extractOperation2(Arrays.asList("Paras","Suriender","Sham"), s->s.length()));
    
/* Lamda and annonymous function accessing variables outside of local space
* Closure -a closure is an instance of a function that can reference nonlocal variables of that
function with no restrictions
*/
          
int a=10,b=20;
Thread thread1 = new Thread(new Runnable() {
           public void run() {
           System.out.println(a+b);
     }
     });
thread1.start();
          
Thread thread = new Thread(()-> System.out.println(a));
thread.start();
          
          
Function<String, Integer> stringToInteger =(String s) -> Integer.parseInt(s);
System.out.println(stringToInteger.apply("12346"));
          
// Method Reference , Class :: static method
Function<String, Integer> stringToInteger2 =Integer::parseInt;
System.out.println(stringToInteger2.apply("4123"));
          
}

     public static List<Integer> performOperation(List<Integer> list, Function<Integer, Integer> function) {
           List<Integer> result = new ArrayList<Integer>();
           for(Integer p : list){
                result.add(function.apply(p));
           }
           return result;
     }

     public static List<Integer> extractOperation(List<String> list, Function<String, Integer> function) {
           List<Integer> result = new ArrayList<Integer>();
           for(String p : list){
                result.add(function.apply(p));
           }
           return result;
     }

     public static List<Integer> extractOperation2(List<String> list, ToIntFunction<String> function) {
           List<Integer> result = new ArrayList<Integer>();
           for(String p : list){
                result.add(function.applyAsInt(p));
           }
           return result;
     }
}

Output:

2 4 6 8
[4, 8, 12, 16]
[5, 9, 4]
[5, 9, 4]
30
10
12346
4123


No comments:

Post a Comment