Slide 1

Slide 1 text

Functional Programming in Kotlin - Android DroidJam India 2018 Rivu Chakraborty

Slide 2

Slide 2 text

Rivu Chakraborty • A learner • Sr. Software Engineer (Android) @ Indus Net Technologies Pvt. Ltd. • Community Lead: GDG Kolkata, Kotlin Kolkata • Instructor @ Caster.io • Author – Reactive Programming in Kotlin • Author – Functional Kotlin • Author – Coroutines for Android Developers (WIP) • Author – Hands on Data Structures & Algorithms in Kotlin (WIP) @rivuchakraborty

Slide 3

Slide 3 text

50% off on EBook 15% off on Print Book RPKFK50 RPKFK15 https://www.packtpub.com/application- development/reactive-programming-kotlin https://www.packtpub.com/application- development/functional-kotlin

Slide 4

Slide 4 text

What is Functional Programming? • Representation of Mathematical Function State Traditional / Imperative / Von Neuman Style Input Output

Slide 5

Slide 5 text

Why Functional Programming • Modulation of code • FP encourages safe ways of programming through Immutability & Pure Functions • Testing becomes easier • Programs are easier to read.

Slide 6

Slide 6 text

What are Functional Languages? • Any language that gives support to Functions as first class citizens are called Functional Language.

Slide 7

Slide 7 text

Pure Functions • Same return value for same input, can be cached • Doesn’t modify anything outside function scope (No side effects) fun square(n:Int):Int { return n*n }

Slide 8

Slide 8 text

Immutability • Immutability != Read Only • Kotlin doesn’t force Immutability, it encourages Immutability • The val keyword doesn’t guarantees immutability

Slide 9

Slide 9 text

Lambda • To be generic, lambda or lambda expressions generally means anonymous functions, that is, functions without names, which can be assigned to variables, passed as arguments, or returned from another function fun invokeSomeStuff(doSomeStuff:()->Unit) { doSomeStuff() } fun main(args: Array) { invokeSomeStuff { println("doSomeStuff called"); } }

Slide 10

Slide 10 text

Arrow • Functional companion to Kotlin’s Standard Library

Slide 11

Slide 11 text

Type Constructors • Type Constructors are mainly theoretical concept. It’s not a Kotlin class constructor building a type. • We can reference Generic Parameterised Types as Type Constructors List Map Option Either

Slide 12

Slide 12 text

Type Constructors • Option • Either • Try

Slide 13

Slide 13 text

Type Classes • Type classes encode behaviour • You can use typeclasses as a DSL to access new free functionality for an existing type, or treat them as an abstraction placeholder for any one type that can implement the typeclass. The extension functions are scoped within the typeclass so they do not pollute the global namespace! • Example: Monads, Functors, Applicatives

Slide 14

Slide 14 text

Monads, Functors, Applicatives

Slide 15

Slide 15 text

This Photo by Unknown Author is licensed under CC BY-NC-ND @rivuchakraborty Rivu Chakraborty