Kotlin is all about simplifying the development. Kotlin’s standard library is full of useful classes and functions which aim to fulfill this promise even where the built-in language features don’t exist. One of such groups of functions in standard library are Kotlin’s higher order functions apply, let, run, with and also.
These functions allow you to write more concise, simpler code by removing code duplication. Warning! Once you start using them, you won’t be able to imagine coding without them!
This post contains all the code that’s been written in this YouTube video.
fun main(args: Array<String>){
val firstPerson = Person("John", 20, "Programmer")
val secondPerson = Person("Dave", 30, "Bus Driver")
val olderPerson = if (firstPerson.age >= secondPerson.age) firstPerson else secondPerson
olderPerson.printPerson()
run {
if (firstPerson.age >= secondPerson.age) firstPerson else secondPerson
}.printPerson()
with(firstPerson) {
age += 1
"Age is now $age"
}.println()
firstPerson.run {
age += 1
"Age is now $age"
}.println()
firstPerson.let { modifiedPerson ->
modifiedPerson.age += 1
"Age is now ${modifiedPerson.age}"
}.println()
secondPerson.apply {
age += 1
job = "Hot-dog seller"
}.printPerson()
secondPerson.also {
it.age += 1
it.job = "YouTuber"
}.printPerson()
}
data class Person(var name: String,
var age: Int,
var job: String) {
fun printPerson() = println(this.toString())
}
fun String.println() = println(this)

Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good. https://www.binance.info/id/register-person?ref=GJY4VW8W
Your article helped me a lot, is there any more related content? Thanks!
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://accounts.binance.info/en-NG/register-person?ref=YY80CKRN
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?