5. Functional programming is a paradigm that allows programming using
expressions i.e. declaring functions, passing functions as arguments and
using functions as statements.
It also simplifies the creation of concurrent programs. Concurrency (or
parallel processing) is vital for improving application performance.
Functional programming
5
6. In functional programming, there are two very important rules:
Immutable state: It means a data object should not be changed after it is
created.
Pure function: always returns the same result for the same arguments.
Rules of functional programming
6
7. ● Usage of functions as input to and output from other functions,
higher order functions.
● Usage of map , filter , and reduce type functions instead of looping.
● Immutable state.
● Recursion in place of looping.
● Composing functions from other functions.
● Distinguishing “pure” functions from functions with side effects.
Advantages of functional programming
7
10. ❏ Lambda expressions are the most talked feature of Java 8.
❏ you could think about lambda expressions as a way of supporting functional
programming in Java.
❏ To assign a lambda expression you need to have a Functional Interface.
Lambda expressions
10
11. Is a interface with a single abstract method
interface Welcome {
abstract void welcome(String string);
}
Functional interface
11
@FunctionalInterface annotation is optional.
13. Let’s say you have a list of numbers :
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9);
If you were asked to print the numbers from the list?
for (Integer number : numbers) {
System.out.println(number);
}
Lambda expression (2)
13
14. We Will can use aggregate operations.
numbers.forEach(number -> System.out.println(number));
We can still improve it further using method reference
operator.
numbers.forEach(System.out::println);
Lambda expression (3)
14
15. Stream represents a sequence of objects from a source ( array,
collection or from database ).
With streams. You can use lambda expressions with more functions,
called aggregate operations like:
ForEach, Reduce, Filter, Map, flatMap, Collect, Count...
Streams Api
15
16. Intermediate operations such as filter() return a new stream on which further
processing can be done. Terminal operations, such as forEach(), mark the stream
as consumed.
A stream pipeline consists of a stream source, followed by zero or more
intermediate operations, and a terminal operation.
Method Types and Pipelines
16
17. When even you have a problem with null parameter think of Optional.
It’s the best solution to use Java 8 feature.
Optional class
17
20. 20
Java 8 introduced @FunctionalInterface, an interface that has
exactly one abstract method. The compiler will treat any interfaces
meeting the definition of a functional interface as a functional interface;
it means the @FunctionalInterface annotation is optional.
Functional Interface
21. 21
In Java 8, BiFunction is a functional interface; it takes two
arguments and returns an object.
BiFunction interface
T – Type of the first argument to the function.
U – Type of the second argument to the function.
R – Type of the result of the function.
22. 22
In Java 8, Consumer is a functional interface; it takes an argument and
returns nothing.
Consumer Interface
23. 23
Let us see the six basic function interfaces.
Functional Interface
25. 25
❏ Use Lambda Expressions: anonymous function.
❏ But you never use this: -> {.....
...}
It should be one line.
❏ Extract heavy lambda into named::methods
Clean code: Flat lambda
27. 27
❏ No nullable parameters (use Optional )
❏ No optional parameters
Instead: thaOption.map(myfunction).
❏ Avoid returning null, you should throw exception or return Optional:
Clean code : parameters
28. 28
❏ You must suffer if you work with non-runtime exceptions
❏ You can use libraries like jool,lombok : rethrow checked as
runtime exception.
Clean code: Exceptions
30. 30
❏ Functions and type over classes.
❏ Composition over inheritance.
❏ Purity over mutability.
❏ Options over nulls.
❏ Runtime over checked exceptions.
❏ Don’t iterate use map, reduce, filter…
❏ Remove extra wrappings with .flatMap
❏ Only use a stream once.
Best practices
Functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.
Pure functions only: Functional code is idempotent. A function should return values only based on the arguments passed and should not affect(side-effect) or depend on global state. Such functions always produce the same result for the same arguments.
default and static methods in Interfaces.
Functional Interfaces and Lambda Expressions.
Java Stream API for Bulk Data Operations on Collections.
Java Time API.
Optional class
Checked exception makes your code ugly by adding of try-catch-finally block.