SlideShare a Scribd company logo
Functional Programming on Android: is it possible?
About
• Android Developer @ M4U
• Organizer @ Kotlin Rio Meetup
Github: https://github.com/lalbuquerque
Linkedin: www.linkedin.com/in/lucasalbuquerque
Email: lucas.albuquerque12@gmail.com
TALK “FUNCTIONAL PROGRAMMING ON ANDROID: IS IT POSSIBLE?”
someData.map(it + 2)
.filter(it > 10)
.sort(…)
.reduce(…)
.group(…)
.flatMap(…)
.just(…)
.jointTo(…)
.fold(…)
.slice(…)
.takeLastWhile(…)
EXPECTATION
data class MyObject(val name: String)
REALITY
Functional Programming on Android: is it possible?
Functional Programming on Android: is it possible?
https://msdn.microsoft.com/en-us/library/hh242985(v=vs.103).aspx
"Functional code is characterised by one thing: the absence of
side effects. It doesn’t rely on data outside the current
function, and it doesn’t change data that exists outside the
current function. Every other “functional” thing can be derived
from this property. Use it as a guide rope as you learn.”
- - Mary Rose Cook
- https://maryrosecook.com/blog/post/a-practical-introduction-to-functional-
programming
SO I CAN GO FUNCTIONAL
WITH JAVA?
Lisp | John McCarthy and Steve Russell | 1958
Immutability
Read-Modify-Write
class Car(var noOfDrivers: Int)
Functional Programming on Android: is it possible?
class Car(val noOfDrivers: Int)
Functional Programming on Android: is it possible?
Using functions right
Functional Programming on Android: is it possible?
"A pure function is a function that, given the same input, will
always return the same output and does not have any
observable side effect."
- Professor Franklin Risby's
- Mostly Adequate Guide to Functional Programming
fun add(x: Int): Int {
val y: Int = readNumFromFile()
return x + y
}
Impure
fun add(x: Int, y: Int) = x + y
Pure
fun plus2(x: Int): Int {
val y = x + 2
cache(y)
return y
}
Impure
fun plus2(x: Int) = x + 2
Pure
"The name of a variable, function or class should answer the
big questions such as why it exists, what it does, how it is
used."
Aiden Mc Raft commenting Robert C. Martin’s Clean Code: A
Handbook of Agile Software Craftsmanship
Demystifying functions
fun add(x: Int, y: Int) = x + y
val add = fun add(x: Int, y: Int) = x + y
fun doSomething(function: (Int, Int) -> Int) {
…
}
data class User(val name: String,
val amount: Int,
val onClick: (User) -> Unit)
with(user) {
holder.itemView.setOnClickListener { onClick(this) }
}
enum class Operation { ADD, SUB }
fun applyOperation(operation: Operation): (Int, Int) -> Int {
val add = fun (x: Int, y: Int) = x + y
val sub = fun (x: Int, y: Int) = x - y
when (operation) {
Operation.ADD -> return add
Operation.SUB -> return sub
}
}
applyOperation(Operation.ADD) (1, 3)
Functional Operators
public inline fun <T, R> …Iterable<T>.map(transform: (T) -> (R)): …List<R>
val myList = listOf(2, 4, 6, 8, 10, 12)
val myNewList = myList.map { it * 2 }
// [ 4, 8, 12, 16, 20, 24 ]
none(…)
val myList = listOf(2, 4, 6, 8, 10, 12)
myList.none { it % 7 == 0 }
// true
filter(…)
val myList = listOf(4, 8, 12, 16, 20, 24)
myList.filter { it > 20 }
// [ 24 ]
forEach(…)
val myList = listOf(2, 4, 6, 8, 10, 12)
myList.forEach { Log.i(TAG, it.toString() }
fun returnBiggerNames(names: MutableList<String>): MutableList<String> {
names
.filter { it.length < 10 }
.forEach { names.remove(it) }
return names
}
Impure
fun returnBiggerNames(names: List<String>) = names.filter { it.length > 10 }
Pure
public List<ContributorCount> getContributorCounts(List<ArticleView> articleViews) {
HashMap<String, Integer> contributorCounts = new HashMap<String, Integer>();
for (ArticleView articleView : articleViews) {
Integer count = contributorCounts.get(articleView.contributor);
if (count == null)
contributorCounts.put(articleView.contributor, 1);
else
contributorCounts.put(articleView.contributor, count + 1);
}
List<ContributorCount> result = new ArrayList<ContributorCount>();
for (String contributor : contributorCounts.keySet()) {
int count = contributorCounts.get(contributor);
if (count > 1)
result.add(new ContributorCount(contributor, contributorCounts.get(contributor)));
}
return result;
}
fun getContributorCounts(views: List<ArticleView>) = views.groupBy { it.contributor }
.mapValues { it.value.size }
  .filter { it.value > 1 }
.map { ContributorCount(it.key, it.value) }
Recursion
fun cosFixpoint(x: Double = 1.0): Double {
while (true) {
val y = Math.cos(x)
if (x == y) return y
x = y
}
}
tailrec fun cosFixpoint(x: Double = 1.0): Double {
val y = Math.cos(x)
return if (x == y) x else cosFixpoint(y)
}
tailrec
fun f(x: Double = 1.0): Double = if (x == cos(x)) x else f(cos(x)))
Thank you!
Bibliography
1. https://drboolean.gitbooks.io/mostly-adequate-guide
2. https://www.theguardian.com/info/developer-blog/2014/dec/
11/functional-android
3. https://medium.com/@anupcowkur/functional-programming-
for-android-developers-part-3-f9e521e96788
4. https://blog.plan99.net/kotlin-fp-3bf63a17d64a

More Related Content

What's hot (20)

DOCX
C program to implement linked list using array abstract data type
loyola ICAM college of engineering and technology
 
DOC
Final ds record
Ganisius Ganish
 
PPTX
My favorite slides
Mitchell Wand
 
PDF
20180721 code defragment
Chiwon Song
 
PPT
Supstat nyc subway
Vivian S. Zhang
 
PDF
Logging in JavaScript - Part-5
Ideas2IT Technologies
 
PDF
Scalaエンジニアのためのモナド入門
Takashi Imahiro
 
PDF
Tweaking the interactive grid
Roel Hartman
 
PDF
20181020 advanced higher-order function
Chiwon Song
 
PDF
Internal workshop es6_2015
Miguel Ruiz Rodriguez
 
ODP
Functors, applicatives, monads
rkaippully
 
PPTX
random forest regression
Akhilesh Joshi
 
KEY
Excelマクロはじめの一歩
Ayumu Hanba
 
PDF
20180310 functional programming
Chiwon Song
 
PDF
Fast and Simple Statistics with Scala
xxx nell
 
PDF
The Ring programming language version 1.6 book - Part 183 of 189
Mahmoud Samir Fayed
 
PPTX
Yin Yangs of Software Development
Naveenkumar Muguda
 
C program to implement linked list using array abstract data type
loyola ICAM college of engineering and technology
 
Final ds record
Ganisius Ganish
 
My favorite slides
Mitchell Wand
 
20180721 code defragment
Chiwon Song
 
Supstat nyc subway
Vivian S. Zhang
 
Logging in JavaScript - Part-5
Ideas2IT Technologies
 
Scalaエンジニアのためのモナド入門
Takashi Imahiro
 
Tweaking the interactive grid
Roel Hartman
 
20181020 advanced higher-order function
Chiwon Song
 
Internal workshop es6_2015
Miguel Ruiz Rodriguez
 
Functors, applicatives, monads
rkaippully
 
random forest regression
Akhilesh Joshi
 
Excelマクロはじめの一歩
Ayumu Hanba
 
20180310 functional programming
Chiwon Song
 
Fast and Simple Statistics with Scala
xxx nell
 
The Ring programming language version 1.6 book - Part 183 of 189
Mahmoud Samir Fayed
 
Yin Yangs of Software Development
Naveenkumar Muguda
 

Similar to Functional Programming on Android: is it possible? (20)

PDF
Functional Programming in Kotlin for Android Developers
Mohsen Mirhoseini
 
PDF
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Hardik Trivedi
 
PDF
Functional programming in Scala
Damian Jureczko
 
PDF
Introduction to Kotlin
Patrick Yin
 
PPTX
Kotlin
BoKaiRuan
 
PDF
Functional Programming 101 for Java 7 Developers
Jayaram Sankaranarayanan
 
PDF
Learning Functional Programming Without Growing a Neckbeard
Kelsey Gilmore-Innis
 
PPTX
Introduction to kotlin
Shaul Rosenzwieg
 
PDF
Beyond PITS, Functional Principles for Software Architecture
Jayaram Sankaranarayanan
 
ODP
From object oriented to functional domain modeling
Codemotion
 
PDF
From object oriented to functional domain modeling
Mario Fusco
 
PDF
Kotlin 101 Workshop
Pedro Vicente
 
PPTX
Kotlin collections
Myeongin Woo
 
PDF
Pure Kotlin Devoxx PL 2021
Jarek Ratajski
 
PDF
Fp for the oo programmer
Shawn Button
 
PPTX
Intro to Functional Programming
Jordan Parmer
 
PDF
科特林λ學
彥彬 洪
 
ODP
Functional programming
S M Asaduzzaman
 
ODP
The hidden mystery behind Scala functional programming
Knoldus Inc.
 
PDF
A Functional Approach to Java: Augmenting Object-Oriented Java Code with Func...
romergalbowx
 
Functional Programming in Kotlin for Android Developers
Mohsen Mirhoseini
 
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Hardik Trivedi
 
Functional programming in Scala
Damian Jureczko
 
Introduction to Kotlin
Patrick Yin
 
Kotlin
BoKaiRuan
 
Functional Programming 101 for Java 7 Developers
Jayaram Sankaranarayanan
 
Learning Functional Programming Without Growing a Neckbeard
Kelsey Gilmore-Innis
 
Introduction to kotlin
Shaul Rosenzwieg
 
Beyond PITS, Functional Principles for Software Architecture
Jayaram Sankaranarayanan
 
From object oriented to functional domain modeling
Codemotion
 
From object oriented to functional domain modeling
Mario Fusco
 
Kotlin 101 Workshop
Pedro Vicente
 
Kotlin collections
Myeongin Woo
 
Pure Kotlin Devoxx PL 2021
Jarek Ratajski
 
Fp for the oo programmer
Shawn Button
 
Intro to Functional Programming
Jordan Parmer
 
科特林λ學
彥彬 洪
 
Functional programming
S M Asaduzzaman
 
The hidden mystery behind Scala functional programming
Knoldus Inc.
 
A Functional Approach to Java: Augmenting Object-Oriented Java Code with Func...
romergalbowx
 
Ad

Recently uploaded (20)

PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
How Cloud Computing is Reinventing Financial Services
Isla Pandora
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
How Cloud Computing is Reinventing Financial Services
Isla Pandora
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Ad

Functional Programming on Android: is it possible?