Thursday 20 April 2023

Functional Programming - Why Bother?

So I have been discussing functional programming with a colleague at work, specifically concerning streams and collections, and he mentions that most of the times he finds a for-loop easier to read than the whole stream().filter().etc.

Of course, this opinion is very much a subjective point of view and depends on your experience with software design, I think.

So I started thinking where the functional programming style with collections and streams offer advantages.

Inversion of Control

Let us say you need to do something with (possibly) all the items in a list.

The iterative style would be to tell the list to give us (one by one) all its items (in a forloop), that we may do something with them.

The declarative style is to tell the list to do something with its items and give us the result.

This is the inversion of control, the execution of the task switches to another party (the list).

If you think about it, it's quite silly to ask a list to provide us with all its items. The list contains all the items already, it is better if we let the list do the task, to keep execution at the place where it makes the most sense, where the items actually are.

After all, the list is the one who probably knows the most efficient way to wrangle with its contents.

Example

The following example shows both a foreach-loop and a stream implementation.

First a little setup code:

The foreach loop would look as follows:

The stream version would look as follows:

Example

An interesting example can be seen in reference [1], where a stream() actually talks to a database. This way the List or Stream is responsible for proper talking to the database, and we do not get bothered with SQL statements.

If you think about it, then a stream().filter().filter().filter().sort(x).findFirst() can actually be mapped to an SQL statement (select * from x where filter1 and filter2 and filter3 order by x limit 1)

Conclusions

So one of the interesting conclusions we can draw is that instead of imperative programming (telling the computer how to do it), we can switch to declarative programming (telling the computer what to do, and letting the computer figure it out).

And it kind of struck a spark with me.

Things work similarly in real life. You can ask your husband to do the grocery shopping, and you provide him a shoppinglist.

But most of the time, we also add expected "behaviour" (functions). Like, buy the eggs at the greengrocer and not in the supermarket, also get some gas, and go to the superstore first, as it closes the soonest. That sort of thing.

And I see this happening more and more.

If I were to draw this to its inevitable conclusion, I guess we'll end up via Machine Learning all the way to advanced AI, where we tell computers what to do, instead of having to specify HOW to do it.

Naturally, this is going to blow up in our face a couple of times. For example, when the AI decides to do something in an illogical way, but that just means there's an unfortunate gap in the training model.

And it makes me wonder, that, in the future my programming skills will be used, no longer to tell the computer how to do things, no longer to tell the computer what to do, but telling the computer when it does something, what I expect the outcome to be (i.e. writing/changing training models.)

Which is not a problem for me. The way I see it, my profession will not disappear, it will simply change like it has in the past and will in the future.

It's up to me to change with it.

References

[1] Express JPA Queries as Java streams
https://piotrminkowski.com/2021/07/13/express-jpa-queries-as-java-streams/

Friday 14 April 2023

KotlinConf 2023

So I was able to attend my first KotlinConf1.

My experience with Kotlin is fairly new. I enjoy programming in both Java and Kotlin. I decide on a case by case basis what I pick.

Our current code base is a mixed bag of the two.

Sessions

I attended the following sessions on April 13 2023:

Opening Keynote2
I got some interesting news on the Kotlin version 2.0. It's coming.
Kotlin & Functional Programming: pick the best, skip the rest (Urs Peter)
Finally a nice talk about functional programming, a clear explanation of the illusive Monad, and what the advantages are of functional programming and which way we're going. Also took a look at Arrow3 for more idioms with functions.
Dissecting Kotlin: Unsealing the Sealed, the SAM, and Other Syntax (Huyen Tue Dao)
What is idiomatic Kotlin? It gave a great overview of the different features there are already in Kotlin, and how they should be used, and what little things we need to pay attention to when we do use them.
Replacing SQL with Kotlin's 'dataframe' on the Las Vegas Strip (Andrew Goldberg)
A nice talk which explains well what a dataframe is. And that with dataframes you can basically do all the operations that I am used to performing on (relational) databases with SQL, but a lot faster, as you're doing things locally? Coupling different data sources together.
Confetti: building a Kotlin Multiplatform conference app in 40min (John O'Reilly, Martin Bonnin)
A good example of using GraphQL and Compose to multiplatform create different mobile apps. Interestingly, they decided not to use Compose for the IPhone, as using Compose apparently causes different UI behaviour (Android behaviour) on the IPhone. Also extremely interesting use of livetemplates.
The Changing Grain of Kotlin (Nat Pryce, Duncan McGregor)
An interesting talk by some folks who wrote a book ("Java to Kotlin: A Refactoring Guidebook"). The talk was about how Kotlin has evolved and how this impacts old code, how it shapes new code, and how it all interacts.

Sessions I would like to have seen if I had the time:

Writing backend APIs in a functional programming style (James Lamine)
Transforming Farmer's Lives Using Android in Kenya (Harun Wangereka)

I attended the following sessions on April 14 2023:

Six Impossible Things (Kevlin Henney)
Massively entertaining talk about software design in general, and some of its history.
Coroutines and Loom behind the scenes (Roman Elizarov)
Very informative talk, basically centering on what the reason was for creating Loom and what the reason was for creating Coroutines and why they can co-exist.
KotlinX Libraries (Anton Arhipov, Svetlana Isakova)
Very informative talk, basically the extended libraries you can use to make your application independent of platform. Serialization, Coroutines, Immutable collections, and more.
How we’re improving performance of IntelliJ IDEA Kotlin plugin (Vladimir Dolzhenko)
Very interesting, contains some tidbit on why some of the things in the plugin are so difficult, as well as how much Kotlin leans on the Java AST and the compiler.
Kotlin Native for CLIs (Ryan Harter)
An explanation on what things to use and what problems you encounter and some benchmarks on creating native commandline tools using Kotlin. In this case it was "differ" a fairly simple tool for compare two images.
Gilded Rose Refactoring Kata (Dmitry Kandalov)
A refactoring kata primarily done with the standard refactoring possibilities for Kotlin in the IntelliJ IDEA. The Kata is from github5 created by Emily Bache6.

Sessions I would like to have seen if I had the time:

Handling billions of events per day with Kotlin Coroutines (Florentin Simion, Rares Vlasceanu)
Evolving your Kotlin API painlessly for clients

I think all sessions will be viewable sometime on the youtubes.

References

[1] KotlinConf 2023
https://kotlinconf.com/
[2] KotlinConf 2023 - Keynote
https://www.youtube.com/watch?v=c4f4SCEYA5Q
[3] Arrow brings idiomatic functional programming to Kotlin
https://arrow-kt.io/
[4] http4k - The Functional toolkit for Kotlin HTTP applications
https://www.http4k.org/
[5] github.com - Gilded Rose Refactoring Kata by Emily Bache
https://github.com/emilybache/GildedRose-Refactoring-Kata
[6] Emily Bache
https://github.com/emilybache