Skip to content

Instantly share code, notes, and snippets.

View optimho's full-sized avatar
😀
I may be slow to respond.

Michael du Plessis optimho

😀
I may be slow to respond.
View GitHub Profile
@optimho
optimho / lambda.kt
Created January 12, 2022 02:35
Examples Of lambda functions
fun main(args: Array<String>) {
// val lambdaName:(InputType, InputType , ..) -> OutputType = {arguementN:InputType, argumentN:InputType -> body}
// greeter ("Michael", "duplessis")
// "Hello Michael du Plessis"
@optimho
optimho / ExtensionFunction.kt
Created January 10, 2022 21:17
You can extent a library function in Kotlin
fun main(args: Array<String>) {
val name="michael du Plessis"
println(name.initials())
}
fun String.initials():String{
val values:List<String> =this.split(' ')
val firstLetter:String = values[0].substring(0,1)
@optimho
optimho / TryCatchFinally.kt
Created January 8, 2022 21:28
Make use of the finally satement in the code
import java.lang.Exception
import java.time.temporal.TemporalAmount
import kotlin.reflect.typeOf
fun main(args: Array<String>) {
val p:Person = Person("Michael", 18)
println("Try Check if age throws an exception")
try {
@optimho
optimho / try.kt
Last active January 8, 2022 21:11
the try catch block to check and handle potential errors
import java.lang.Exception
import java.time.temporal.TemporalAmount
import kotlin.reflect.typeOf
fun main(args: Array<String>) {
val p:Person = Person("Michael", 10)
println("Try Check if age throws an exception")
try {
@optimho
optimho / throwException.kt
Created January 8, 2022 20:55
Create and throw an exception
import java.time.temporal.TemporalAmount
import kotlin.reflect.typeOf
fun main(args: Array<String>) {
val p:Person = Person("Michael", 10)
if (p.age<12){
throw InvalidAgeException(p.age, "WTF dude")
}
@optimho
optimho / cast.kt
Created January 8, 2022 20:35
A way of safe casting
import java.time.temporal.TemporalAmount
import kotlin.reflect.typeOf
fun main(args: Array<String>) {
val obj: Any = getStuff("1")
val casted: Int? = obj as? Int
println(casted)
}
@optimho
optimho / any.kt
Created January 8, 2022 20:25
returning an any type structure and casting it
import java.time.temporal.TemporalAmount
import kotlin.reflect.typeOf
fun main(args: Array<String>) {
val obj: Any = getStuff("5")
val casted: Persons = obj as Persons
println(casted.name)
}
@optimho
optimho / Main.kt
Created January 8, 2022 20:12
type Checking in Kotlin
import java.time.temporal.TemporalAmount
import kotlin.reflect.typeOf
fun main(args: Array<String>) {
val obj: Any = Persons("Bob")
if (obj is String) {
println("It is")
} else {
@optimho
optimho / nulls.kt
Created January 8, 2022 09:54
Some more information on using requireNotNull command
import java.util.concurrent.TimeUnit
import kotlin.random.Random
import kotlin.system.measureNanoTime
fun main(args: Array<String>) {
var name: String? = "Donn"
// Tenary operator
// kotlin doe not have a teneary operator
// so you can not do this
@optimho
optimho / checkTheNulls.kt
Created January 8, 2022 09:39
This snippit is an example of how to deal and check for nulls
import java.util.concurrent.TimeUnit
import kotlin.random.Random
import kotlin.system.measureNanoTime
fun main(args: Array<String>) {
var name: String? = "Donn"
// Tenary operator
// kotlin doe not have a teneary operator
// so you can not do this