This year I’ve been at Scala Days 2017 and because I’m a functional programer noob I attended all beginners’s talks.
One talk that really got my attention and gave me the the intuition of the power of functional programming was Easy and Efficient Data Validation with Cats.
In her talk Daniela gave an example of how functional abstractions can help us developer, even if we don’t know (yet) what a Monad is.
Some months laters I’ve discovered Arrow:
Functional datatypes & abstractions for Kotlin
and I started to study it, to grok some functional concepts and play with Android.
Ok cool, but what has to do all of this with Android?
In this serie of posts I’d like to replicate Daniela’s talk using kotlin and Android.
We are going to build a simple form with two inputs: - Mail - Phone Number and validate it.
We are going to achieve this using different data types, improving the current solution in each step choosing a new data type.
Wait, what is a data type?
Quoting the (excelent) arrow documentation, a data type is:
A datatype is a class that encapsulates one reusable coding pattern. These solutions have a canonical implementation that is generalised for all possible uses. Some common patterns expressed as datatypes are absence handling with Option, branching in code with Either, catching exceptions with Try, or interacting with the platform the program runs in using IO.
Don’t worry if you don’t understand the definition right now, we are going to use three of them we will discuss them.
Demo application structure
We are going to use one activity and one viewmodel for each data type, sharing the same xml view with two different TextInputLayout.
The input of our validation will be a data class with two strings.
To validate mail and phone number we are going to use two pure functions in a kotlin file.
Enough for first post, we can start to see some real code in following posts:
The source code for this article is available here.