You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _moreReadMe/kotlin_basics/README.md
+375Lines changed: 375 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -854,3 +854,378 @@ fun main(args : Array<String>) {
854
854
As you can see, the constructor parameter n and a are being used to initialized their respective properties.
855
855
856
856
**Data classes** are kotlins way of providing concise immutable data types... it is going to generate equals hashcoded to string... they will be considered equal if data they contain is equal... also, they give us effective copy construcors... x.copy()
857
+
858
+
859
+
860
+
861
+
862
+
863
+
864
+
865
+
866
+
867
+
868
+
869
+
870
+
871
+
872
+
873
+
874
+
875
+
876
+
---
877
+
878
+
879
+
880
+
881
+
882
+
883
+
884
+
```kotlin
885
+
sayHello1()
886
+
sayHello2()
887
+
888
+
funsayHello1(): Unit { println("Hello")}
889
+
890
+
funsayHello2() { println("Hello")}
891
+
892
+
fungetGreeting(): String="Hello Kotlin"//single expression function
893
+
fungetGreeting() ="Hello Kotlin"//also, single expression function
894
+
895
+
funsayHello3(itemToGreet:String) {
896
+
val msg ="Hello "+ itemToGreet // or val msg = "Hello $itemToGreet"
companionobject { // can be rename like "comapanion object Factory"
1047
+
funcreate() =Entity("id")
1048
+
}
1049
+
}
1050
+
1051
+
val entity =Entity.Companion.create() // same as Entity.create(), writing COmpanion is not required
1052
+
val entity =Entity.Factory.create() // suppose we have named it Factory
1053
+
1054
+
companion objects are like any other class, an implement interfaceor inherit or have properties or methods
1055
+
1056
+
object declaration is a convenient way of creating threaad safe singletons within kt
1057
+
1058
+
object EntityFacctory {
1059
+
funcreate() =Entity("id")
1060
+
}
1061
+
classEntity (valid:String) {
1062
+
}
1063
+
1064
+
enumclassEntityType {
1065
+
EASY, MEDIUM, HARD
1066
+
}
1067
+
1068
+
val id =UUID.randomID().toString()
1069
+
1070
+
sealed classes allow us too define related classhierarhies... for eg. can be used where result is "success or failure" or "easy, medium or hard"
1071
+
with sealed classes we can have different propeerties (key diff bw enum & sealed)... compiler will use smart casting
1072
+
1073
+
1074
+
sealedclassEntity() {
1075
+
data classEasy(valid:String, valname:String): Entity() // data classes
1076
+
data classMedium(valid:String, valname:String): Entity()
1077
+
data classHard(valid:String, valname:String, valmultiplier:Float): Entity()
1078
+
}
1079
+
1080
+
fun Entity.Medium.printInfo() { // extension function
1081
+
...
1082
+
}
1083
+
1084
+
funprintFilteredStrings(list:List<String>, predicate: (String) ->Boolean) { // higher order funstion
1085
+
list.forEach {
1086
+
if(predicate(it)) {
1087
+
println(it)
1088
+
}
1089
+
}
1090
+
}
1091
+
1092
+
val list =listOf("Kotlin", "Java", "C++", "Javascript")
1093
+
printFilteredStrings(list, {it.startsWith("K")}) // we have passed our lambda withing parenthesis
1094
+
1095
+
printFilteredStrings(list) { // we can take use of lambda syntax, same as above
1096
+
it.startsWith("K")
1097
+
}
1098
+
1099
+
val list =listOf("Kotlin", "Java", "C++", "Javascript", null, null)
1100
+
list
1101
+
.filterNotNull()
1102
+
.take(3) // or takeLast(3) to take that many elements
1103
+
.filter {
1104
+
it.startsWith("J")
1105
+
}
1106
+
.map {
1107
+
it.length
1108
+
}
1109
+
.forEach {
1110
+
println(it)
1111
+
}
1112
+
1113
+
output:4or10 (for last 3)
1114
+
1115
+
list
1116
+
.filterNotNull()
1117
+
.associate {it t it.length}
1118
+
.forEach {
1119
+
println("${it.value}, ${it.key}")
1120
+
}
1121
+
1122
+
val map = list
1123
+
.filterNotNull()
1124
+
.associate {it t it.length}
1125
+
1126
+
the kt standard lib also provides a variety of funtohelpuspulloutindividualelementsfromacollection
1127
+
1128
+
vallanguage = list.first() // or .last()
1129
+
1130
+
val language = list.filterNotNull().find{it.startsWith("Java")} // or .findLast {}
1131
+
1132
+
strings in kt have a useful funcalledorEmpty
1133
+
vallanguage = list.filterNotNull().find{it.startsWith("foo")}.orEmpty() // to default collection to empty instead of null
1134
+
1135
+
kt has 1st classsupp for fun, inluding fun types and higher order fun... and kt standard lib builds upon those tools and provides a rich set of fun operator for us to use... this allows us to build powerful functional chains to transform our data and make complex workflows much simpler
1136
+
1137
+
1138
+
1139
+
1140
+
1141
+
1142
+
1143
+
model data
1144
+
Kotlin proj default
1145
+
--
1146
+
1147
+
top level variables and functions & varandfunassociatedwithclass
1148
+
--
1149
+
1150
+
toString(), hashCode(), ...
1151
+
1152
+
we have extension function or extension properties on existing class
1153
+
1154
+
higher order funtions & functional data type
1155
+
1156
+
invoke() method....
1157
+
1158
+
functional types...
1159
+
1160
+
these types of functions will be useful in click & event listeners etc
0 commit comments