![Page 1: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/1.jpg)
Why KOTLIN is my Favourite example of Functional
Programming ?
Jayashree S Kumar, IBM
![Page 2: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/2.jpg)
Agenda
• What ? Kotlin, Functional Programming
• Why ? Kotlin , Function Programming
• My Faviroute FP concepts with Kotlin
• Conclusion
![Page 3: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/3.jpg)
About Me
• Java Classes Library developer
• Worked Extensively on JDK’s Testing
• Runtimes team @ IBM Software Labs
![Page 4: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/4.jpg)
WHAT ?
![Page 5: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/5.jpg)
• Statically Typed
• JVM Targeted
• Open Source
• OO & FP features
• 100% Interoperable with Java
Borrowed: https://www.slideshare.net/abreslav/introduction-to-kotlin-brief-and-clear
![Page 6: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/6.jpg)
• Functions first class citizens
• Immutable data
• Mathematical(Pure) functions
• Easier to write, reuse & test
• No loops => lots of recursion, less efficient.
![Page 7: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/7.jpg)
Hello, World!
//Top-level functions //Optional “ ; ” . no "new"// Types on the right // Strong type inference // Val & Var(traditional way) to declare types
Compile error: Val cannot be reassigned
![Page 8: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/8.jpg)
WHy?
![Page 9: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/9.jpg)
• Exists since 1958(LISP)… Haskell (1990), OCaml (1996), Scala(2004), Clojure(2007)
• Highly parallel multicore architectures -> high concurrency -> immutability in code design
• Better compilers and runtimes -> more expressive
• Because Java got it to mainstream!
Fact: Whatsapp needs only 50 engineers for its 900M user because Erlang is used to implement its concurrency needs. Facebook uses Haskell in its anti-spam system.
FP now?
![Page 10: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/10.jpg)
˜3.4 million apps on
Google Play
PYPL - 16th rank, SOF - least disliked, RedMonk - Raising.
Kotlin ?Modern, Concise,
SAFE
Top-Notch Tools
SIMPLE learning curve
![Page 11: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/11.jpg)
How?
![Page 12: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/12.jpg)
Lambda
4
![Page 13: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/13.jpg)
Where Lambda?
Execute a block of code later—> once or multiple times.
- Run code in separate thread.- Run it multiple times- Run only when necessary - Run it at specific point in an algorithm Eg. comparison operation while sorting- Run code when some event happens Eg. on click of a link
![Page 14: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/14.jpg)
Higher-Order Functions
![Page 15: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/15.jpg)
10 20
Building blocks of HOF
![Page 16: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/16.jpg)
Closure
0 1 2 3 0 1 4
![Page 17: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/17.jpg)
Extension Function
abc = (3, 2, 1)
![Page 18: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/18.jpg)
Composition, Currying
= 25
= 6
![Page 19: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/19.jpg)
Lazy Evaluation
![Page 20: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/20.jpg)
Convert Java File to Kotlin File
CTRL + ALT + SHIFT + K (or CMD + ALT + SHIFT + K)
Command-line
> vi HelloWorld.kt > kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar > java -jar Helloworld.jar
![Page 21: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/21.jpg)
Conclusion• FP is a journey .
• Kotlin is making your life easier to on-board by making usage of functional concepts easier..
• Java, owing to its massive adoption, bears much more responsibility while making changes…
• Happier to use my Java and Kotlin in conjunction!! :)
![Page 22: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/22.jpg)
![Page 23: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/23.jpg)
![Page 24: Why KOTLIN is my Favourite example of Functional Programming€¦ · Scala(2004), Clojure(2007) • Highly parallel multicore architectures -> high concurrency -> immutability in](https://reader033.vdocuments.net/reader033/viewer/2022042310/5ed74d71c079a632805806e2/html5/thumbnails/24.jpg)
Thank U!