Skip to content

Instantly share code, notes, and snippets.

Avatar

Magda Miu magdamiu

View GitHub Profile
View CollectionsVsSequences.kt
var someNumbers = listOf(1, 4, 5, 6, 7, 8, 2323, 5456, 343, 2)
someNumbers
.filter { it % 2 == 0 }
.map { it * 3 }
.sum()
someNumbers
.asSequence()
.filter { it % 2 == 0 }
.map { it * 2 }
@magdamiu
magdamiu / SealedClass.kt
Created Dec 17, 2021
Sealed class in Kotlin
View SealedClass.kt
sealed class Fruit(val name: String) {
class Apple : Fruit("Apple")
class Mango : Fruit("Mango")
}
class Pomegranate : Fruit("Pomegranate")
fun display(fruit: Fruit) {
when (fruit) {
is Fruit.Apple -> println("${fruit.name} is good for iron")
@magdamiu
magdamiu / StringTemplates.kt
Created Dec 17, 2021
String templates | High performance with idiomatic Kotlin
View StringTemplates.kt
val displayContentWithTemplate = "Hello $firstName! Please confirm that $email is your email address."
@magdamiu
magdamiu / UseFunction.kt
Created Dec 17, 2021
Disposable pattern to avoid resource leaks | High performance with idiomatic Kotlin
View UseFunction.kt
inputStream.use {
outputStream.use {
// do something with the streams
outputStream.write(inputStream.read())
}
}
// improved option
arrayOf(inputStream, outputStream).use {
// do something with the streams
@magdamiu
magdamiu / Immutability.kt
Created Dec 17, 2021
Immutability | High performance with idiomatic Kotlin
View Immutability.kt
interface ValueHolder<V> {
val value: V
}
class IntHolder : ValueHolder<Int> {
override val value: Int
get() = Random().nextInt()
}
fun main() {
val sample = IntHolder()
println(sample.value) //260078462
View CollectionsAndSequences.kt
fun main() {
smallList()
smallSequence()
}
fun smallList() = (0..5)
.filter { print("list filter($it) "); it % 2 == 0 }
.map { print("list map($it) "); it * it }
.first()
@magdamiu
magdamiu / ReifiedTypes.kt
Created Dec 17, 2021
inline and reified types | High performance with idiomatic Kotlin
View ReifiedTypes.kt
inline fun <reified T> printTypeName() {
println(T::class.simpleName)
}
fun main() {
printTypeName<String>()
println(String::class.simpleName)
}
@magdamiu
magdamiu / Crossinline.kt
Created Dec 17, 2021
crossinline modifier | High performance with idiomatic Kotlin
View Crossinline.kt
inline fun <T, R : Comparable<R>> Iterable<T>.sortedBy(crossinline selector: (T) -> R?): List<T> {
return sortedWith(compareBy(selector))
}
@magdamiu
magdamiu / InlineFunctions.kt
Last active Dec 17, 2021
inline and noinline modifiers | High performance with idiomatic Kotlin
View InlineFunctions.kt
inline fun computeValues(
number: Int, doubleValue: (number: Int) -> Unit,
noinline tripleValue: (number: Int) -> Unit) {
doubleValue.invoke(number)
tripleValue.invoke(number)
}
fun main() {
val number = 7;
@magdamiu
magdamiu / Lambdas.kt
Created Dec 12, 2021
Lambdas expressions & Closure (capturing lambda) | High performance with idiomatic Kotlin
View Lambdas.kt
var priceBooks = 0.0
val prefixPriceDetails = "The current sum is "
books.forEach {
priceBooks += it.price
println("$prefixPriceDetails $priceBooks")
}