Writing functional code with Java  8

Sponsored by:
Sponsored by:

Samil Vargas

Payment System Developer at @Wirecard

Sponsored by:

FP with Java 8

What we are going to see ?
  1. What is functional programming
  2. Pros/Cons of using functional programming
  3. How to implement functional programming
Sponsored by:

FP with Java 8

Functional programming is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. Functional programming is declarative rather than imperative, and application state flows through pure functions. Contrast with object oriented programming, where application state is usually shared and colocated with methods in objects.

Sponsored by:

FP with Java 8

Functional programming is a programming paradigm, meaning that it is a way of thinking about software construction based on some fundamental, defining principles (listed above).

Sponsored by:

FP with Java 8

Imperative

VS

Declarative

Sponsored by:

FP with Java 8

Imperative: In which programmer instruct the machine how to change its state.

 

 

Declarative: in which programmer merely declares properties of the desired properties.

Sponsored by:

FP with Java 8

  • Imperative programming is a programming paradigm that uses statements that change a program’s state.

 

  • Declarative programming is a programming paradigm … that expresses the logic of a computation without describing its control flow.
Sponsored by:

FP with Java 8

Sponsored by:

FP with Java 8

Task: Make a Java function that return whether a number is prime or not

Sponsored by:

FP with Java 8

Descriptive Way

public static boolean isPrime(int number) {
	return number > 1 &&	
		IntStream.range(2, (int)Math.sqrt(number))
			.noneMatch(index -> number % index == 0);
}
Sponsored by:

FP with Java 8

Sponsored by:

FP with Java 8

Imperative Way

public static boolean isPrime(int number) {
	for (int i = 2; i * i <= number; i++) {
		if (number % i == 0) {
			return false;
		}
	}
	return number > 1;
}
Sponsored by:

FP with Java 8

Sponsored by:

FP with Java 8

Text

Clarifications

Sponsored by:

FP with Java 8

A lot of people in Java-land are trying to mix traditional imperative Java code and functional-ish code, with varying degrees of success. Java is not a functional language, it is fundamentally an Object Oriented language that allows us to adopt some functional concepts in so far as we enforce their correct implementation through developer discipline. Unlike in Haskell, Idris, Ocaml (and even Scala) the compiler alone won’t save us.

Sponsored by:

FP with Java 8

Pros and Cons

Sponsored by:

FP with Java 8

Functional Programming Pros

Some of the issues that are solved by using functional programming instead of OOP are the followings:

  • Pure functions
  • Race Condition
Sponsored by:

FP with Java 8

Object-Oriented Programming Pros

Some of the issues that are solved by using object-oriented programming instead of FP are the followings:

  • More understandable code
  • Easier to debug
Sponsored by:

FP with Java 8

Sponsored by:

FP with Java 8

deck

By Samil Vargas