Skip to content

Instantly share code, notes, and snippets.

@Ayeeta
Ayeeta / lambda.kt
Created August 4, 2020 09:00
About lamda expressions
val area = {l: Int, w: Int -> l * w}
println(area(6,4))
val areaOption :(Int, Int) -> Int = {l, w -> l*w}
println(areaOption(6,4))
@Ayeeta
Ayeeta / classes.kt
Created August 4, 2020 07:57
About classes and inheritance in kotlin
open class Car(val make: String, val model: String, val engineSize: String){
open fun accelerate(){
println("vroom vroom")
}
fun brake(){
println("STOP")
}
fun reverse(){
@Ayeeta
Ayeeta / nulls.kt
Created August 3, 2020 20:03
About nulls in kotlin
var nullable : String? = null
println(nullable?.length)
var notNullableExample : String = "something"!!
println(notNullableExample)
@Ayeeta
Ayeeta / hashMaps.kt
Created August 3, 2020 19:48
About hash maps / dictionaries
/**
Immutable hash map/dictionary
**/
val contacts = mapOf("Tom" to "0780","Peter" to "4784","Derrick" to "6780" )
println(contacts)
println(contacts["Tom"])
println(contacts.get("Peter"))
println(contacts.values)
/**
@Ayeeta
Ayeeta / collections.kt
Created August 3, 2020 08:12
About collections - mutable
/**
Mutable lists/arrays use arrayListOf() method
Notice it has other methods to add to the list
**/
val mutableAlphabetsLists = arrayListOf("c","d","b","a")
println(mutableAlphabetsLists)
println(mutableAlphabetsLists.size)
println(mutableAlphabetsLists.add("x"))
println(mutableAlphabetsLists)
@Ayeeta
Ayeeta / collections.kt
Created August 3, 2020 08:08
About collections in Kotlin
/**
Immutable lists/arrays uses the listOf() method
together with a few methods for operations on lists/arrays
**/
val alphabets = listOf("c","d","b","a")
println(alphabets)
println(alphabets.sorted())
println(alphabets[2])
println(alphabets.contains("b"))
println(alphabets.last())
@Ayeeta
Ayeeta / when.kt
Created August 1, 2020 22:13
when statement
var x = 1
when (x){
1 ->println("x is 1")
else -> println("x is not equal to 1")
}
@Ayeeta
Ayeeta / funtions.kt
Created August 1, 2020 21:45
Functions in Kotlin
//Example with no args
fun exampleFunction(){
println("This is an example of a function in Kotlin")
}
//Example with Args. Remember the data types are declared explicitly
fun exampleFunctionWithArgs(someString:String){
println(someString)
}
@Ayeeta
Ayeeta / numbersAndOperators.kt
Created August 1, 2020 21:26
Numbers and operators
val a = 3
val b = 2.4
println(a + b)
println(a / b)
println(a * b)
println(a % b)
@Ayeeta
Ayeeta / Strings.kt
Created August 1, 2020 21:02
About strings in kotlin
//Escaped strings, means you can use escaped characters
val escapedString = "Kotlin is fun because Zombie said \"Kotlin is ...\" "
println(escapedString)
/**
output - Kotlin is fun because Zombie said "Kotlin is ..."
Here's a ist of escape characters you could use
\t - Inserts tab