Kotlin Standard Library (let, run also, apply, with)

Brijesh Singh
Nov 2 · 2 min read

Introduction:

Kotlin implementing some of the important standard library functions They provide us with useful higher-order functions implementing idiomatic patterns.

Why should we use a standard library in Kotlin?

The standard library will make programming so easier, faster and readability.

Prerequisites:

You should have good knowledge of Kotlin’s null check and familiar with a lambda function.

Let’s Start:

The above graph will always remind you of the uses of the standard library functions.

Five standard libraries:

  1. let takes the object it is invoked upon as the parameter and returns the result of the lambda expression and also returned as an argument.
/**
* Created by SinghBrijesh on 11/02/2019.
*/
fun main(args: Array<String>) {
val name = "Brijesh Kumar"
name.let {
println("$it Singh ")
val length = name.length
print(length)
}
}
//Prints
//Brijesh Kumar Singh
//13

let also check for null as shown above.

/**
* Created by SinghBrijesh on 11/02/2019.
*/
fun main(args: Array<String>) {
var name : String? = "Brijesh"
name?.let { println(it) } //prints Brijesh
name = null
name?.let { println(it) } //prints nothings
}

2.run used to override values

/**
* Created by SinghBrijesh on 11/02/2019.
*/
fun main(args: Array<String>) {
var tutorial = "Brijesh"
println(tutorial) //Brijesh
tutorial = run {
tutorial = "This is run function"
tutorial
}
println(tutorial) //This is run function
}

3. also expressions do some additional processing on the object it was invoked.
Unlike let, it returns the original object instead of any new return data. Hence the return data has always the same type.

/**
* Created by SinghBrijesh on 11/02/2019.
*/
fun main(args: Array<String>) {
data class Person(var name: String, var tutorial: String)
val person = Person("Brijesh", "Kotlin")val l = person.let { it.tutorial = "Android" }
val
al = person.also { it.tutorial = "Android" }
println(l) // prints kotlin.Unit
println
(al) //prints Person(name=Brijesh, tutorial=Android)
println
(person) //prints Person(name=Brijesh, tutorial=Android)

4.apply is an extension function on a type. It runs on the object reference into the expression and returns the object reference on completion.

/**
* Created by SinghBrijesh on 11/02/2019.
*/
fun main(args: Array<String>) {
data class Student(var name: String, var rollNumber: Int, var department: String, var address: String)
val student = Student("Brijesh", 26, "cse", "Jamshedpur")
student.apply {
name
= "Saroj"
rollNumber
= 92
department = "it"
address
= "Banglore"
}
print(student)// prints Student(name=Saroj, rollNumber=92, department=it, address=Banglore)
}

5. with is used to change instance properties without the need to call dot operator over the reference every time. it takes an argument.

/**
* Created by SinghBrijesh on 11/02/2019.
*/
fun main(args: Array<String>) {
data class Student(var name: String, var rollNumber: Int, var department: String, var address: String)
val student = Student("Brijesh", 26, "cse", "Jamshedpur")
with(student) {
name
= "Saroj"
rollNumber
= 92
department = "it"
address
= "Banglore"
}
print(student)// prints Student(name=Saroj, rollNumber=92, department=it, address=Banglore)
}

Brijesh Singh

Written by

Software Developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade