28
Functional Programming In JAVA By Harmeet Singh(Taara) (Java EE Developer) Email: [email protected] Blog: http://harmeetsingh13.blogspot.in Skype: harmeetsingh0013 Contact: Via Email or Skype

Functional programming in java 8 by harmeet singh

Embed Size (px)

DESCRIPTION

Cover Basic concept for Functional Programming in Java. Define new functional interfaces, lambda expressions, how to translate lambda expression, JVM deal with new byte code etc. This is not the perfect slides for functional programming, but trying cover simple basic functional programming.

Citation preview

Page 1: Functional programming in java 8 by harmeet singh

Functional Programming In JAVA

By Harmeet Singh(Taara) (Java EE Developer)

Email: [email protected]: http://harmeetsingh13.blogspot.in

Skype: harmeetsingh0013Contact: Via Email or Skype

Page 2: Functional programming in java 8 by harmeet singh

➢ Introduction➢ Functional Programming. ➢ Single Abstract Method(SAM).➢ Functional Interface.➢ Declare Lambda Expressions.➢ Code Refactor Using Lambda Expression. ➢ Predefined Functional Interfaces in Java 8.➢ User Defined Functional Interface in Java 8.➢ Type Inference. ➢ Translation Of Lambda Expressions.➢ Runtime Translation Strategies. ➢ Leftover: The Things We Didn’t Cover.

Contents

Page 3: Functional programming in java 8 by harmeet singh

Acknowledgement

❖ Thanks To My Parents.

❖ Thanks To All Who Support Me or Not.

❖ Dedicated To My Teacher “Mr. Kapil Sakhuja”.

❖ Thanks to Insonix.

Page 4: Functional programming in java 8 by harmeet singh

Introduction

In Java 8, the functional programming is the important evolution in Object Oriented Programming world of Java. Now, Java use lambdas, Simple Abstract Method (SAM) etc for allow functional programming in Object Oriented World. There are so many new features are added in Java 8 like Streams, Default methods etc. But Today’s we only discuss about “Functional Programming In Java”.

Page 5: Functional programming in java 8 by harmeet singh

What is Functional Programming ?

Immutable Object: In Object Oriented and Functional Programming, An Immutable Object is an object whose state cannot be modified after it is created.

Functional Programming: Functional Programming involves writing code that does not change state. One of the key feature of Functional Programming is the First-Class Functions.

Page 6: Functional programming in java 8 by harmeet singh

Single Abstract Method(SAM)

Interfaces have one Abstract Method are called SAM.

In Java there are lots of interfaces which only have one abstract method(by default interface have abstract method) like Runnable, Closeable etc.

These Single Abstract Method(SAM) Interfaces are also called “Functional Interface”.

Page 7: Functional programming in java 8 by harmeet singh

Functional Interface

Functional Interface: In Java 8, If Functional Interface have more than one methods, one is different and rest of the methods have same signature of Object class methods. These types of Interfaces also called Functional Interfaces like Comparator etc in Java. If interface have one abstract method and one or more than one Default and Static methods are also called Functional Interface.

Page 8: Functional programming in java 8 by harmeet singh

Declare Lambda Expression

Syntax : lambda = ArgList “->” BodyArgList = Identifier && Body = ExpressionExamples:

(int x, int y) -> { return x+y }(x, y) -? { return x+y }(x) -> { return x+1 }() -> { System.out.println(“Hello World”) }

Page 9: Functional programming in java 8 by harmeet singh

Code Refactoring Using Lambda

Page 10: Functional programming in java 8 by harmeet singh

Predefined Functional Interfaces in Java 8

In Java 8, There are lots of Predefined Functional Interface, But today’s we only cover some important Functional interfaces are :-

Page 11: Functional programming in java 8 by harmeet singh

Predefined Functional Interfaces in Java 8

Predicate<T>: Represents a predicate (boolean-value-function) of one argument. Example:

Page 12: Functional programming in java 8 by harmeet singh

Predefined Functional Interfaces in Java 8

Consumer<T>: Represents an operation that accept a single input argument and returns no result. Example:

Page 13: Functional programming in java 8 by harmeet singh

Predefined Functional Interfaces in Java 8

Function<T, R>: Represents a function that accept a one argument and produces a result.Example:

Page 14: Functional programming in java 8 by harmeet singh

Predefined Functional Interfaces in Java 8

Supplier<T>: Represents a supplier of resultsExample:

Page 15: Functional programming in java 8 by harmeet singh

Predefined Functional Interfaces in Java 8

UnaryOperator<T>: Represents an operations on a single operands that produces a result of the same type as its operand. Example:

Page 16: Functional programming in java 8 by harmeet singh

Predefined Functional Interfaces in Java 8

BinaryOperator<T, T>: Represents an operations upon two operands of same type, producing a result of the same type as the operands.Example:

Page 17: Functional programming in java 8 by harmeet singh

User Defined Functional Interfaces In Java 8

IN Java 8, it is also possible to create our custom user define Functional Interface with the help of “@FunctionalInterface” annotation. Our Functional Interfaces also have default methods and static methods. @FunctionalInterface: @FunctionalInterface annotation indicates that the type declaration is intended to be a functional interface, as defined by Java Language Specification.

Page 18: Functional programming in java 8 by harmeet singh

User Defined Functional Interfaces In Java 8

Example:

Page 19: Functional programming in java 8 by harmeet singh

Type Inference

Type Inference is not the new topic in Java. In Java 7 we create the Collections object with the help of Diamond Operator(<>) like :

List<Integer> list = new ArrayList<>();Under the hood of this code, Compiler use the “Type Inference” technique. In this compiler pick the type information from the left side of type declaration.

Page 20: Functional programming in java 8 by harmeet singh

Type Inference

For Lambda expressions in Java 8, compiler use the same “Type Inference” technique with some improvements. Examples:

Predicate<Integer> predicate = (x) -> x > 5; //code compile

Predicate predicate = (x) -> x > 5; // Compile time error// Predicate is a raw type. References to generic type //Predicate<T> should be parameterized

Page 21: Functional programming in java 8 by harmeet singh

Translation Of Lambda Expression

Translate the lambda expression to bytecode is major challenge for Java, because the important thing is to maintain backward compatibility in bytecode. There are so many challenges were faced for conversion like:

1. How to deal with Functional Interfaces?2. For Lambdas use Inner classes?3. Maintain Lambda information at runtime? etc.

Page 22: Functional programming in java 8 by harmeet singh

Translation Of Lambda Expression

There are so many things are used and maintain for lambdas in JVM, But for JVM the lambdas are not a new, Because some languages are already use JVM for Runtime Environment that have “Lambda Expression” like Groovy, Scala, Clojure etc.

Today we only discuss the brief steps for translate Lambda Expression after compilation.

Page 23: Functional programming in java 8 by harmeet singh

Translation Of Lambda Expression

Followings are the Steps: ➔ Read “Lambda Expression” and desugars the

lambda body into a method whose argument list and return type match that of “Lambda Expression”.

Example : list.forEach( s -> { System.out.println(s); } );

//before compile

static void lambda$1(String s) { //After compile System.out.println(s); }

Page 24: Functional programming in java 8 by harmeet singh

Translation Of Lambda Expression

Followings are the Steps:➔ At the point at which the “Lambda Expression”

would be captured, it generates an “invokedynamic” CallSite like :

list.forEach( s -> { System.out.println(s); } );

list.forEach( [lambda for lambda$1 as Block] );

Page 25: Functional programming in java 8 by harmeet singh

Translation Of Lambda Expression

Followings are the Steps:➔ This CallSite is called Lambda Factory for Given

Lambda. ➔ The Dynamic Arguments to the lambda factory

are the values captured from lexical scope. ➔ The Bootstrap Method for “Lambda Factory” is

called “Lambda Metafactory”.

Page 26: Functional programming in java 8 by harmeet singh

Runtime Translation Strategies

➔ Generate Inner Classes Dynamically.➔ Generate per-SAM wrapper class

◆ One per SAM type, not per lambda expression◆ Use method handles for invocation.◆ Use ClassValue to cache wrapper for SAM.

➔ Use Dynamic Proxies.➔ Use MethodHandleProxies.asInterfaceInstance.➔ Use VM private API to build object from scratch.

Page 27: Functional programming in java 8 by harmeet singh

Leftover: The Important Topics We Didn’t Cover

1. Lexical Scoping2. Method References 3. Method Handlers 4. Default Methods and Static methods in Interface5. Streams in Java 86. Java 8 Date API7. invokedynamic8. Concurrency in lambda9. Advanced Collections 10.Design and Architecture Principles

Page 28: Functional programming in java 8 by harmeet singh

References:

❖ Special Thanks to Richard Warburton for “Java 8 lambdas”.

❖ “Implementing Lambda Expressions in Java” by Brian Goetz.

❖ “Lambda Expressions in Java” by Simon Ritter.❖ Oracle Java 8 Api Docs.❖ Translation Of Lambda Expression by OpenJDK.(

http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.html)

And many more like, DZone, StackOverflow etc.