Skip to content

Commit c511724

Browse files
authored
Add files via upload
Learn how and where to use scope functions in kotlin. Also, explore 'with' vs 'apply' vs 'run' vs 'also' vs 'let' higher-order functions with their Lambda expression usage. Explore each function with their properties such as whether they return 'Context Object' or 'lambda result'. And also whether the context object can be accessed by using either 'this' keyword or 'it' identifier.
1 parent 5e60ba5 commit c511724

5 files changed

+122
-0
lines changed

src/49_with_scope_function.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Person {
2+
var name: String = "Sriyank Siddhartha"
3+
var age: Int = 26
4+
}
5+
6+
fun main() {
7+
8+
/** Scope Function: 'with'
9+
Property 1: Refer to context object by using 'this'
10+
Property 2: The return value is the 'lambda result' */
11+
12+
val person = Person()
13+
14+
val bio: String = with(person) {
15+
println(name)
16+
println(age)
17+
age + 5
18+
"He is a freak who loves to teach in his own way" // will be returned and stored in 'bio' String variable
19+
}
20+
21+
// println("Age after five years is $ageAfterFiveYears")
22+
println(bio)
23+
}

src/50_apply_scope_function.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Person {
2+
var name: String = ""
3+
var age: Int = 0
4+
}
5+
6+
fun main() {
7+
8+
/** Scope Function: 'apply'
9+
Property 1: Refer to context object by using 'this'
10+
Property 2: The return value is the 'context object' */
11+
12+
val person = Person().apply {
13+
name = "Sriyank Siddhartha"
14+
age = 26
15+
}
16+
17+
with(person) {
18+
println(name) // prints Sriyank Siddhartha
19+
println(age) // prints 26
20+
}
21+
22+
// Perform some other operations on 'person' object
23+
person.also {
24+
it.name = "Shreks from Smartherd"
25+
println("New name: ${it.name}") // prints New name: Shreks from Smartherd
26+
}
27+
}

src/51_also_scope_function.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
fun main() {
4+
5+
/** Scope Function: 'also' 'ALSO PERFORM THE FOLLOWING EXTRA OPERATION'
6+
Property 1: Refer to context object by using 'it'
7+
Property 2: The return value is the 'context object' */
8+
9+
// Initialise numbersList
10+
val numbersList: MutableList<Int> = mutableListOf(1, 2, 3)
11+
12+
// Some other code... may be a function call or program to swap numbers (doesn't matter what code)
13+
14+
// Operations on the 'numbersList'
15+
val duplicateNumbers = numbersList.also {
16+
println("The list elements are: $it")
17+
it.add(4)
18+
println("The list elements after adding an element: $it")
19+
it.remove(2)
20+
println("The list elements after removing an element: $it")
21+
}
22+
23+
// duplicateNumbers will be same as numbersList
24+
println("Original numbers: $numbersList")
25+
println("Duplicate numbers: $duplicateNumbers")
26+
}

src/52_let_scope_function.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
fun main() {
3+
4+
/** Scope Function: 'let'
5+
Property 1: Refer to context object by using 'it'
6+
Property 2: The return value is the 'lambda result' */
7+
8+
// Use 'let' function to avoid NullPointerException
9+
10+
val name: String? = "Hello"
11+
12+
// Execute the lambda expression only if the 'name' variable is NOT NULL
13+
val stringLength = name?.let {
14+
println(it.reversed())
15+
println(it.capitalize())
16+
it.length // Will be returned and stored within stringLength variable
17+
}
18+
19+
println(stringLength)
20+
}

src/53_run_scope_function.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
class Person {
3+
var name: String = "Sriyank Siddhartha"
4+
var age: Int = 26
5+
}
6+
7+
fun main() {
8+
9+
/** Scope Function: 'run'
10+
Property 1: Refer to context object by using 'this'
11+
Property 2: The return value is the 'lambda result' */
12+
13+
// 'run' is combination of 'with' and 'let'
14+
// If you want to operate on a Nullable object and avoid NullPointerException then use 'run'
15+
16+
val person: Person? = Person()
17+
18+
val bio = person?.run {
19+
println(name)
20+
println(age)
21+
age + 5
22+
"He is a freak who loves to teach in his own way" // will be returned and stored in 'bio' variable
23+
}
24+
25+
println(bio)
26+
}

0 commit comments

Comments
 (0)