Kotlin
Functional programming basic
Julian
Overview
- Functional Programming
- Collection and Sequence
Functional programming
● A programming paradigm (like OOP)
● Declarative programming
● Programs are constructed by applying and composing functions
(source: wiki)
Declarative
programming
Imperative
programming
Concept
● Functions as first-class citizens
● Avoid shared and mutable state
(source:
https://blog-c7ff.kxcdn.com/blog/wp-content/uploa
ds/2016/10/black.png)
Advantage
● No side effect, easy for testing and
concurrency
● Readability and simplicity
Functions as first-class citizens
(first-class function)
Ex. Higher-order functions : function as return value or as argument
Avoid shared and mutable state
No change, immutable in input
FP
● Data
● Math
OOP
● State
● Object to real word
Collection and
Sequence
Iterator
Collections
https://kotlinlang.org/docs/reference/collectio
ns-overview.html
Constructing Collections
● listOf<T>
● setOf<T>
● mutableListOf<T>
● mutableSetOf<T>
● mapOf<T>
● mutableMapOf()
Constructing Collections
Constructing Sequences
● From elements
● From Iterable
● From function
● From chunks
https://kotlinlang.org/docs/reference/sequenc
es.html#constructing
generateSequence
0, 2, 4, 6, 8,10...
1 2 3 4 5 1 2 3 4 5
map{x -> x+3}
take(3)
collection sequence
toList
1 2 3 4 5 1 2 3 4 5
4 5 6 7 map{x -> x+3}
take(3)
collection sequence
toList 4
8
1 2 3 4 5 1 2 3 4 5
4 5 6 7
4 5
map{x -> x+3}
take(3)
collection sequence
toList 4 5
8
6
1 2 3 4 5 1 2 3 4 5
4 5 6 7
4 5
map{x -> x+3}
take(3)
collection sequence
toList 4 5 6
6
8
Collection vs Sequence
When to Use Sequences
https://kotlinlang.org/docs/reference/sequenc
es.html#sequence-processing-example
Performance
● measureTimeMills example in terminalOperation.kt
● https://gist.github.com/ajalt/c6c120c7cc13a49bc2a8bf62d433d455
What will be printed?
Eager vs Lazy loading
Eager vs Lazy loading
Eager vs Lazy loading
Sequence doesn’t produce result at this moment
Sequence: intermediate and terminal operation
intermediate and terminal operation
See comments in source code
Kotlin Collections and Collection Extension Functions
http://jussi.hallila.com/Kollections/
https://kotlinlang.org/docs/reference/collectio
n-operations.html#common-operations
● Operators
● Transformers
● Aggerations
● Checks and Actions
● Filters
Operators
● Plus
● Minus
Transformers
● Map
● Sorted / SortedByDescending
● Flatten
● Zip
● Reversed
Aggregators
● GroupBy
● Chunked
● Average/Max/Min/Sum
● Reduce/Fold
Checks and Actions
● ForEach
● Any/All/ None
● Contains
Filters
● Filter
● Take
● First/Last/Single (FirstOrNull…..)
● Distinct
Hints: Terminal operation
● ToList
● Sorted
Hints: Simplicity
Reference
● Code for challenges https://github.com/Julian-Chu/kotlin-collection-sequence
● https://typealias.com/guides/when-to-use-sequences/
● https://kotlinlang.org/docs/reference/collections-overview.html
Links
https://winterbe.com/posts/2018/07/23/kotlin-sequence-tutorial/ (by Gx)
https://www.educba.com/functional-programming-vs-oop/ (by Gx)
https://www.guru99.com/functional-programming-tutorial.html (by Gx)
https://medium.com/@przemek.materna/curried-kotlin-48166885ddfa (by Jimin Hsieh)
https://arrow-kt.io/docs/0.10/effects/io/ (by Jimin Hsieh)

Kotlin functional programming basic@Kotlin TW study group