Skip to content

Instantly share code, notes, and snippets.

Avatar

Elijah Ayeeta

View GitHub Profile
@Ayeeta
Ayeeta / lambda.kt
Created Aug 4, 2020
About lamda expressions
View lambda.kt
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 Aug 4, 2020
About classes and inheritance in kotlin
View classes.kt
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 Aug 3, 2020
About nulls in kotlin
View nulls.kt
var nullable : String? = null
println(nullable?.length)
var notNullableExample : String = "something"!!
println(notNullableExample)
@Ayeeta
Ayeeta / hashMaps.kt
Created Aug 3, 2020
About hash maps / dictionaries
View hashMaps.kt
/**
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 Aug 3, 2020
About collections - mutable
View collections.kt
/**
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 Aug 3, 2020
About collections in Kotlin
View collections.kt
/**
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 Aug 1, 2020
when statement
View when.kt
var x = 1
when (x){
1 ->println("x is 1")
else -> println("x is not equal to 1")
}
@Ayeeta
Ayeeta / funtions.kt
Created Aug 1, 2020
Functions in Kotlin
View funtions.kt
//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)
}
View numbersAndOperators.kt
val a = 3
val b = 2.4
println(a + b)
println(a / b)
println(a * b)
println(a % b)
@Ayeeta
Ayeeta / Strings.kt
Created Aug 1, 2020
About strings in kotlin
View Strings.kt
//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
You can’t perform that action at this time.