Functional programming

with Java & VAVR

#EYD

Juan Antonio Breña Moral

v0.3.0-SNAPSHOT

Who I am

Juan Antonio Breña Moral (40)
Chapter Lead, Deejay & Mindstorms/STEAM teacher.
Twitter | Github | Linkedin
Purpose: "Riding dragons with innovation & fun"
Quote: "Pressure makes diamonds."
- George S. Patton Jr.

“Production is the best place on the Internet.”

- Josh Long

Agenda

  1. Goals
  2. Java & VAVR
  3. Functional patterns
  4. A roadmap
  5. Sharpening the axe
  6. Future talks

Goals

I wish the audience could:

  1. Use functional composition in the daily job
  2. Design with better Types
  3. Think in Failures, not in Exceptions

Goals

Is Java, a functional language?

  1. Non-functional
  2. Mostly functional
  3. Purely functional

Java & VAVR

In computer science, functional programming is a programming paradigm 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.

Wikipedia

Java & VAVR

Java & VAVR

Java 8+

Java & VAVR

VAVR

Functional patterns

  • Functional composition
  • Type driven development
  • Functional Error Handling

Functional composition

Functional composition

Function composition is a mechanism to combine simple functions to build more complicated ones.

The ability to easily compose functions encourages factoring (breaking apart) functions for maintainability and code reuse.

Type driven development

Type-driven development is a programming style where you write types first and use them to guide the definition of functions in your program.

A strong type system can not only prevent errors, but also guide you and provide feedback in your design process.

Using functional programming language changes the way you write code. You start thinking about the (data) types that represent the data your code works with.

Type driven development

  • Accuracy with Types used for Object design
  • Cardinality
  • Recovered Exceptions or better Types?

Type driven development

Accuracy with Types used for Object design

Values:

Type driven development

Accuracy with Types used for Object design

Effects:

  • Optional is a monad that models the effect of optionality
  • Try is a monad that models the effect of failures (manages exceptions as effects)
  • Either is a monad that models the effect of failure and success

Type driven development

Accuracy with Types used for Object design

Type driven development

Accuracy with Types used for Object design

Type driven development

Cardinality

In mathematics, the cardinality of a set is a measure of the "number of elements of the set".

Type driven development

Recovered Exceptions or better Types?

"The hidden performance costs of instantiating Throwables", Norman Maurer

Functional error handling

We need to minimize the usage of @ControllerAdvice to avoid a GOTO pattern

Functional error handling

In Java 8, the language was released with some Types to increase the expressiveness, like Optional to model optionality in the value returned by a method.

Following the same idea, how to model the Exceptionality? Unfortunately, the language doesn't provide any solution in the line of Optional.

Functional error handling

Using VAVR, you can add 3 new alternatives:

  • Try
  • Either
  • Lifting

Functional error handling

The Try type represents a computation that may either result in an exception, or return a successfully computed value.

Functional error handling

Either represents a value of two possible types. By convention, the Left signifies a failure case result and the Right signifies a success.

A roadmap

Standardized open ladder of functional programming

Level 1

  1. Immutable Data
  2. Second-Order Functions
  3. Function Composition
  4. Lambdas

Level 2

  1. Algebraic Data Types
  2. Pattern Matching
  3. General Recursion
  4. Type Classes, instances, & Laws
  5. Lower-Order Abstractions (Equal, Semigroup, Monoid, etc)
  6. Referential Transparency & Totality
  7. Higher-Order Functions
  8. Partial-Application, Currying

Skills:

  1. Use second-order functions (map, filter, fold) on immutable data structures
  2. Use data types to represent optionality
  3. Solve problems without nulls, exceptions, or type casts
  4. Able to use functional programming “in the small”
  5. Model a business domain with ADTs
  6. Reliably identify & isolate pure code from impure code

A roadmap

Do you want to reach the Level 3?

Further information

Sharpening the axe

  • Euler: Synchronous problems
  • Latency: Asynchronous problems

Future talks

@juanantoniobm

Thanks