Kotlin & Arrow:
The functional way
Noe Luaces
@noe_luaces
● Galician since I have memory (now living
in Barcelona)
● Avid comic book reader since I was a
teenager
● Software Engineer since October 2007
● Kotlin Developer since October 2018
> whoami
What is
Kotlin?
Designed to
interoperate fully with
other JVM-based
languages
Object
Oriented
Kotlin is...
Designed to
interoperate fully with
other JVM-based
languages
Object
Oriented
Great introductory
language to
functional
programming
Functional
Kotlin is...
Designed to
interoperate fully with
other JVM-based
languages
Object
Oriented
Great introductory
language to
functional
programming
Can be used in
backend, frontend or
as a script language
Functional
General
Purpose
Kotlin is...
Designed to
interoperate fully with
other JVM-based
languages
Object
Oriented
Great introductory
language to
functional
programming
Can be used in
backend, frontend or
as a script language
Functional
General
Purpose
Kotlin is...
Android has officially
adopted Kotlin as the
supported language
for mobile
development services
Mobile
Standard
● Developed by JetBrains in 2011
● Its name comes from Kotlin island,
Russia
Kotlin background
● JetBrains lead Dmitry Jemerov said
that they were looking for a language
with the same features as Scala but
with a compilation velocity similar to
Java
Kotlin background
Function type
Function Type
● Functions in Kotlin are first-class values
● They can be assigned to variables and passed around as parameters
Function Type (function reference)
Function Type
Function Type
Function Type
Higher Order
Functions
Higher Order Functions
● Functions that accept parameters typed as a function
● Functions that accept lambda expressions as parameters
Higher Order Functions
● Scope functions like run, apply, let, with, also accept a lambda expression
● Other functions such as map, flatMap, filter, fold
Higher Order Functions
Pure Functions
Pure Functions
● Depend only on the input to produce the result, not on any hidden
information or external state
● Have no observable side effects, like modifying a variable passed by
reference.
● Hence, they are easy to test
Pure Functions
Not pure functions
Immutability
Immutable objects
● Remain in exactly the state in which they were created. Therefore, they are
thread-safe
● Are easier to write, use and they make cleaner code
● Make easier to parallelize the program as there are no conflicts among
objects
Immutable objects
● Are preferred whenever possible in Kotlin
● Are defined with the keyword val and can be initialized only single time
● Data classes, that hold the state which they were created with
Immutable objects
Pattern
matching
The "when {}" block
● Advanced form of the switch-case statement known from Java
● No break statements are needed at the end of each case
The "when {}" block
● Due smartcast, we can access methods or properties of the type without
extra checks
● Smartcast also works with subclasses providing a full pattern matching
● Could be used as an expression or a statement
The "when {}" block
The "when {}" block
Currying
Currying
● Is a mathematical technique
● Consists on breaking down a function that takes multiple arguments into a
series of functions that each take only one argument
Currying
Null safety
Null safety
● Explicit nullable declarations
● Safe calls
● !! operator
Null safety
What is missing?
In Functional Programming, we try to avoid throwing exceptions:
● For methods which fail often and predictably, like parsing user input as a
number, it’s costly and unnecessary to throw exceptions
● Handling checked exceptions can easily make the client’s code needlessly
complicated
What is
Arrow?
Arrow background
● Open source library sponsored by 47 Degrees
● Fusion between KATEGORY and funKTionale, the most
relevant FP libraries for Kotling in 2017
● First release was available in 2018
● Equivalent to Cats for Scala and Vavr for Java
Modeling absence
Data Type: Option
● Avoids NullPointerException, without error detail
● Possible values: None or Some
Data Type: Option
Dealing with Option values: Fold
{() -> "Nothing!"}
{(Book) -> "Book.name found!"}
Dealing with Option values: Fold
Dealing with Option values: Fold
Capturing
Throwable
Data type: Try
● Useful to handle Runtime Exceptions
● Failure or Success
● Can be combined with a when statement that uses smart cast
Data type: Try
Modeling Errors
Data type: Either
● Represents a value with two possibilities
● Left (error case) or Right (happy case)
● Allows to fail fast providing all the context
Data type: Either
Data type: Either
Dealing with Either values
{cat -> IsDeadFailure(cat)}
{ cat -> cat.name}
Dealing with Either values: Bimap
Dealing with Either values: Bimap
Dealing with Either values: Bimap
Dealing with Either values
Modeling Parallel
Errors
Data type: Validated
● Collect all errors at once
● Possible values: Valid or Invalid
● Often used the typealias ValidatedNel ( instead of Validated<Nel>)
Data type: Validated
Data type: Validated
Data type: Validated
To sum up...
KOTLIN ARROW
HIGHER ORDER FUNCTIONS
IMMUTABILITY
PATTERN MATCHING
CURRYING
EITHER
OPTION
TRY
VALIDATIONS
Why Kotlin?
● Concise syntax
● Null safety
● Ease of transition from Java
● Interoperability with other JVM-based
languages
● Introductory language to functional
programming
● Android's adoption as the supported
language
Why Arrow?
● Data types
● Type classes
● Effects
● Optics
● Other functional programming
patterns as higher- level abstractions
ready to use
Noe Luaces
@noe_luaces
Thank you

Kotlin & Arrow the functional way