SlideShare a Scribd company logo
1 of 68
Download to read offline
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

More Related Content

What's hot

Good Weather
Good WeatherGood Weather
Good Weather
dlazcano
 

What's hot (20)

Past tense
Past tensePast tense
Past tense
 
Past Simple and past Continuous (1).ppt
Past Simple and past Continuous (1).pptPast Simple and past Continuous (1).ppt
Past Simple and past Continuous (1).ppt
 
Simple past tense
Simple past tenseSimple past tense
Simple past tense
 
Presentation on Tense
Presentation on TensePresentation on Tense
Presentation on Tense
 
Past perfect
Past perfect Past perfect
Past perfect
 
Grammar function -for - 2nd- back students-06-06-2014
Grammar function -for - 2nd- back students-06-06-2014Grammar function -for - 2nd- back students-06-06-2014
Grammar function -for - 2nd- back students-06-06-2014
 
Negative adjective prefixes
Negative adjective prefixesNegative adjective prefixes
Negative adjective prefixes
 
TENSES in English Grammar
TENSES in English GrammarTENSES in English Grammar
TENSES in English Grammar
 
Easy way to learn tense
Easy way to learn tenseEasy way to learn tense
Easy way to learn tense
 
THE PRESENT PERFECT TENSE
THE PRESENT PERFECT TENSETHE PRESENT PERFECT TENSE
THE PRESENT PERFECT TENSE
 
Verb To Be
Verb To BeVerb To Be
Verb To Be
 
Future simple be going to - future continuous - future perfect simple (gr...
Future simple   be going to -  future  continuous - future perfect simple (gr...Future simple   be going to -  future  continuous - future perfect simple (gr...
Future simple be going to - future continuous - future perfect simple (gr...
 
Present Continuous
Present ContinuousPresent Continuous
Present Continuous
 
Perfect Continuous Tenses
Perfect Continuous TensesPerfect Continuous Tenses
Perfect Continuous Tenses
 
Good Weather
Good WeatherGood Weather
Good Weather
 
Present progressive in the future
Present progressive in the futurePresent progressive in the future
Present progressive in the future
 
Unit 8 perfect infinitive
Unit 8   perfect infinitiveUnit 8   perfect infinitive
Unit 8 perfect infinitive
 
Third Conditional
Third ConditionalThird Conditional
Third Conditional
 
Past Simple
Past SimplePast Simple
Past Simple
 
English tenses explained.pptx
English tenses explained.pptxEnglish tenses explained.pptx
English tenses explained.pptx
 

Similar to Kotlin & Arrow the functional way

Similar to Kotlin & Arrow the functional way (20)

Kotlin & arrow: the functional way
Kotlin & arrow:  the functional wayKotlin & arrow:  the functional way
Kotlin & arrow: the functional way
 
Kotlin - A Programming Language
Kotlin - A Programming Language Kotlin - A Programming Language
Kotlin - A Programming Language
 
A short introduction to the Kotlin language for Java developers
A short introduction to the Kotlin language for Java developersA short introduction to the Kotlin language for Java developers
A short introduction to the Kotlin language for Java developers
 
From Java to Kotlin
From Java to KotlinFrom Java to Kotlin
From Java to Kotlin
 
Kotlin
KotlinKotlin
Kotlin
 
Is this Swift for Android? A short introduction to the Kotlin language
Is this Swift for Android? A short introduction to the Kotlin languageIs this Swift for Android? A short introduction to the Kotlin language
Is this Swift for Android? A short introduction to the Kotlin language
 
Kotlin vs Java • Bapusaheb Patil • TechieAid Talk
Kotlin vs Java • Bapusaheb Patil • TechieAid TalkKotlin vs Java • Bapusaheb Patil • TechieAid Talk
Kotlin vs Java • Bapusaheb Patil • TechieAid Talk
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
 
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
 
Modern Programming Languages - An overview
Modern Programming Languages - An overviewModern Programming Languages - An overview
Modern Programming Languages - An overview
 
Introduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTX
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developers
 
I know Java, why should I consider Clojure?
I know Java, why should I consider Clojure?I know Java, why should I consider Clojure?
I know Java, why should I consider Clojure?
 
Scala-Ls1
Scala-Ls1Scala-Ls1
Scala-Ls1
 
Post-graduate course: Object technology: Implementation of object-oriented pr...
Post-graduate course: Object technology: Implementation of object-oriented pr...Post-graduate course: Object technology: Implementation of object-oriented pr...
Post-graduate course: Object technology: Implementation of object-oriented pr...
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
 
Coding in kotlin
Coding in kotlinCoding in kotlin
Coding in kotlin
 
Coding in kotlin
Coding in kotlinCoding in kotlin
Coding in kotlin
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 

More from Thoughtworks

More from Thoughtworks (20)

Design System as a Product
Design System as a ProductDesign System as a Product
Design System as a Product
 
Designers, Developers & Dogs
Designers, Developers & DogsDesigners, Developers & Dogs
Designers, Developers & Dogs
 
Cloud-first for fast innovation
Cloud-first for fast innovationCloud-first for fast innovation
Cloud-first for fast innovation
 
More impact with flexible teams
More impact with flexible teamsMore impact with flexible teams
More impact with flexible teams
 
Culture of Innovation
Culture of InnovationCulture of Innovation
Culture of Innovation
 
Dual-Track Agile
Dual-Track AgileDual-Track Agile
Dual-Track Agile
 
Developer Experience
Developer ExperienceDeveloper Experience
Developer Experience
 
When we design together
When we design togetherWhen we design together
When we design together
 
Hardware is hard(er)
Hardware is hard(er)Hardware is hard(er)
Hardware is hard(er)
 
Customer-centric innovation enabled by cloud
 Customer-centric innovation enabled by cloud Customer-centric innovation enabled by cloud
Customer-centric innovation enabled by cloud
 
Amazon's Culture of Innovation
Amazon's Culture of InnovationAmazon's Culture of Innovation
Amazon's Culture of Innovation
 
When in doubt, go live
When in doubt, go liveWhen in doubt, go live
When in doubt, go live
 
Don't cross the Rubicon
Don't cross the RubiconDon't cross the Rubicon
Don't cross the Rubicon
 
Error handling
Error handlingError handling
Error handling
 
Your test coverage is a lie!
Your test coverage is a lie!Your test coverage is a lie!
Your test coverage is a lie!
 
Docker container security
Docker container securityDocker container security
Docker container security
 
Redefining the unit
Redefining the unitRedefining the unit
Redefining the unit
 
Technology Radar Webinar UK - Vol. 22
Technology Radar Webinar UK - Vol. 22Technology Radar Webinar UK - Vol. 22
Technology Radar Webinar UK - Vol. 22
 
A Tribute to Turing
A Tribute to TuringA Tribute to Turing
A Tribute to Turing
 
Rsa maths worked out
Rsa maths worked outRsa maths worked out
Rsa maths worked out
 

Recently uploaded

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Recently uploaded (20)

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 

Kotlin & Arrow the functional way