Skip to content

Commit 2bb324e

Browse files
authored
Merge branch 'smartherd:master' into master
2 parents 8e04043 + 0570120 commit 2bb324e

16 files changed

+138
-4
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,21 @@ Learn Kotlin Programming, its basics and Fundamentals from scratch. Follow [this
6262
11. Sorting and Filtering
6363
- "filter" function
6464
- "map" function
65-
- Predicates
66-
12. Conclusion
65+
- Predicates: all, any, find, count.
66+
12. Kotlin NULL Safety
67+
- Safe call
68+
- with Let
69+
- Elvis
70+
- Lateinit keyword
71+
- Lazy delegation and 'lateinit' vs. 'lazy'
72+
13. Scope Functions
73+
- with
74+
- apply
75+
- let
76+
- also
77+
- run
78+
13. Conclusion
6779

6880
## Authors
6981

70-
* **Team Smartherd**
82+
* **Sriyank Siddhartha**
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/8_string_interpolation.kt renamed to src/08_string_interpolation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ class Rectangle {
1616

1717
var length: Int = 0
1818
var breadth: Int = 0
19-
}
19+
}
File renamed without changes.

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)