Skip to content

Instantly share code, notes, and snippets.

@libindev
Last active August 7, 2021 04:13
Show Gist options
  • Save libindev/90f4a94a2b98735781be39ad0a903580 to your computer and use it in GitHub Desktop.
Save libindev/90f4a94a2b98735781be39ad0a903580 to your computer and use it in GitHub Desktop.
Kotlin Basics
package com.libindev.kotlintemplates
fun main(){
//if
val a=5
val b=10
var max=0
if (a < b) max = b
println(max)
max = if (a > b) {
println("Choose a")
a
} else {
println("Choose b")
b
}
println (max)
println("__________________________")
// when replace switch
var x=12
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
}
when (x) {
0, 1 -> print("x == 0 or x == 1")
else -> print("otherwise")
}
//when in range
val validNumbers= intArrayOf(10,12)
when (x) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")// checks x is in valid Numbers Array
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")
}
println("__________________________")
//for
val colors= arrayOf("yellow","green","black","blue","white")
for (item in colors) println(item)
for (i in 1..3) {
println(i)
}
for (i in 6 downTo 0 step 2) {
println(i)
}
for (i in colors.indices) {
println(colors[i])
}
println("__________________________")
//while
x=10
while (x > 0) {
println( x)
x--
}
do {
val y = 10
println("y=$y")
}
while (y != 10) // y is visible here!
fun main(){
fun MutableList<Int>.sumof():Int { // extension function
var sum=0
for (i in this){
sum= sum + i
}
return sum
}
val l= mutableListOf(1, 2, 3,2,8,4)
print( "Sum of array :"+l.sumof())
}
package com.libindev.kotlintemplates
fun powerOf(number: Int, exponent: Int) { } //function notation
fun areaofCirle(radius:Double,PI:Double = 3.14):Double{
return radius*radius*PI
}//function with default argument pie
fun double(x: Int): Int = x * 2 //single expression function
fun main() {
print( areaofCirle(5.0)) // default value for PI
print( " "+areaofCirle(5.0,3.14159))//PI value changed to 3.14159
}
package com.libindev.kotlintemplates
//https://kotlinlang.org/docs/reference/lambdas.html
//(A, B) -> C
val a = { i: Int,j :Int -> j + i } //lambda functions
fun Int.square():Int { return this*this } // extension functions Use:this function as common Util functions in project.
val square_lambda: Int.() -> Int = {this*this}//same function as lambda
val sum: Int.(Int) -> Int = { t -> plus(t)//extension function with arguments
//or this +t
}
fun concatenateAndPrint(func:(String, String)->(String), i:Int){ //higher order function
for( j in 0..i){
var s=func("libin","mathew")
print("$s \n")
}
}
fun main() {
print(a.invoke(55,45)) // use invoke
print(a(55,45))
// or simply a()
print(5.square())
print(5.square_lambda())
10.sum(500)
concatenateAndPrint(
{ s1 :String, s2:String->
s1.decapitalize()
return@concatenateAndPrint s1.capitalize()+" "+s2.capitalize()
},10)
}
package com.libindev.kotlintemplates
//https://kotlinlang.org/docs/reference/null-safety.html
var int:Int=10 //not null
var int2:Int? = null //nullable int
fun main(){
print(int)
print(int2?.plus(10))//print null
print(int2!!.plus(10))//!! operator converts any value to a non-null type and throws an exception if the value is null
print(int2 ?: -1) //Elvis Operator
}
package com.libindev.kotlintemplates
//https://kotlinlang.org/docs/reference/scope-functions.html
//distinguish scope methods
//The way to refer to the context object
//The return value.
//there are five of them: let, run, with, apply, and also.
//Basically, these functions do the same: execute a block of code on an object.
// What's different is how this object becomes available inside the block
// and what is the result of the whole expression.
//apply and also return the context object.
//let, run, and with return the lambda result.
//The return value of apply and also is the context object itself.
//let it Lambda result Yes
//run this Lambda result Yes
//run - Lambda result No: called without the context object
//with this Lambda result No: takes the context object as an argument.
//apply this Context object Yes
//also it Context object Yes
fun main(){
var k= Person ("Mark",10,"Amsterdam").let {
it.age=15
it.city="New York"
return@let it.city
} //
var p1 = Person("Jose",15,"NewYork")
println( p1.let {
it.age=11
it.city="Dubai"
return@let it.city
})
print(p1.city)
var p= Person ("Mark",10,"Amsterdam").apply {
this.age=15
this.city="New York"
} //returns context objects
var d=Person(name = "John", age = 10, city = "Dubai").run {
val s= city
return@run name+s.capitalize()
} // returns lambda expression
val numbers = mutableListOf("one", "two", "three")
val s= numbers.run {
add("four")
add("five")
return@run count {
it.endsWith("r")
it.endsWith("e")
}
}
println("There are $s elements that end with e.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment