Kotlin is very concise language. You can achieve same functionality with fewer characters compared to Java. But as an experienced developer, it was easy for me to switch from Java to Kotlin. Same might not be true for those who are new to programming. I had tough time explaining how Lambda works to a trainee who was new to programming.
For instance, I was trying to explain a simple map function that maps objects of one type to another. I have two simple classes
and a simple usage of map function
I came up with following step by step code to explain what's actually going on. All the code snippets do the exact same thing. Converting imperative to functional code at the end.
Extract the list of journals to new variable.
The map function is being executed here, we can specify the round brackets to be explicit
The code after map function including the curly braces is actually a lambda. It is the last (and only) parameter for the map function. So we move it inside the round brackets.
The variable it is default name of the parameter in the lambda, if the lambda takes exactly one parameter.
We can specify the name explicitly in the definition
The type of the parameter was implicit. As map function is being called on list of Journal, the lambda parameter type is Journal. Below we specify the type for it.
Let's rename it variable so that it is more readable
Now this lambda can be extracted out to a new variable. Below we assign the lambda expression to the new variable and replace the lambda in map function with this new variable
The new variable type again is implicit above. Let's specify it explicitly.
It takes a parameter of type Journal and returns DisplayJournal
As lambda is nothing but a function, let's define it as a function.
We add the fun keyword here. Note that Kotlin functions are first-class, which means that
I can assign those to a variable.
We can also define this function explicitly and use the function reference using ::.
Note that in the function definition we have added return keyword which returns a value
from the function body
We can also convert this function to expression body by removing the curly braces and the
return keyword
The return type that is explicitly defined for the function can be removed in case of
expression body
For instance, I was trying to explain a simple map function that maps objects of one type to another. I have two simple classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Journal(id: String) | |
class DisplayJournal(journal: Journal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfDisplayJournals = listOf(Journal("123")).map { DisplayJournal(it) } |
Extract the list of journals to new variable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfJournals = listOf(Journal("123")) | |
val listOfDisplayJournals = listOfJournals.map { DisplayJournal(it) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfJournals = listOf(Journal("123")) | |
val listOfDisplayJournals = listOfJournals.map() { DisplayJournal(it) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfJournals = listOf(Journal("123")) | |
val listOfDisplayJournals = listOfJournals.map ({ DisplayJournal(it) }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfJournals = listOf(Journal("123")) | |
val listOfDisplayJournals = listOfJournals.map ({it-> DisplayJournal(it) }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfJournals = listOf(Journal("123")) | |
val listOfDisplayJournals = listOfJournals.map ({it: Journal-> DisplayJournal(it) }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfJournals = listOf(Journal("123")) | |
val listOfDisplayJournals = listOfJournals.map ({journal: Journal-> DisplayJournal(journal) }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfJournals = listOf(Journal("123")) | |
val convertJournalToDisplayJournal = { journal: Journal -> DisplayJournal(journal) } | |
val listOfDisplayJournals = listOfJournals.map (convertJournalToDisplayJournal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfJournals = listOf(Journal("123")) | |
val convertJournalToDisplayJournal: (Journal)->DisplayJournal = { journal: Journal -> DisplayJournal(journal) } | |
val listOfDisplayJournals = listOfJournals.map (convertJournalToDisplayJournal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfJournals = listOf(Journal("123")) | |
val convertJournalToDisplayJournal: (Journal)->DisplayJournal = fun(journal: Journal) { | |
DisplayJournal(journal) | |
} | |
val listOfDisplayJournals = listOfJournals.map (convertJournalToDisplayJournal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfJournals = listOf(Journal("123")) | |
fun convertJournalToDisplayJournalFn(journal: Journal): DisplayJournal{ | |
return DisplayJournal(journal) | |
} | |
val convertJournalToDisplayJournal: (Journal)->DisplayJournal = ::convertJournalToDisplayJournalFn | |
val listOfDisplayJournals = listOfJournals.map (convertJournalToDisplayJournal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfJournals = listOf(Journal("123")) | |
fun convertJournalToDisplayJournalFn(journal: Journal): DisplayJournal = DisplayJournal(journal) | |
val convertJournalToDisplayJournal: (Journal)->DisplayJournal = ::convertJournalToDisplayJournalFn | |
val listOfDisplayJournals = listOfJournals.map (convertJournalToDisplayJournal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val listOfJournals = listOf(Journal("123")) | |
fun convertJournalToDisplayJournalFn(journal: Journal) = DisplayJournal(journal) | |
val convertJournalToDisplayJournal: (Journal)->DisplayJournal = ::convertJournalToDisplayJournalFn | |
val listOfDisplayJournals = listOfJournals.map (convertJournalToDisplayJournal) |
Comments