Skip to content

Instantly share code, notes, and snippets.

View magdamiu's full-sized avatar

Magda Miu magdamiu

View GitHub Profile
@magdamiu
magdamiu / Immutability.kt
Created December 17, 2021 13:31
Immutability | High performance with idiomatic Kotlin
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
@magdamiu
magdamiu / CollectionsAndSequences.kt
Created December 17, 2021 12:28
Collections & Sequences
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 December 17, 2021 11:34
inline and reified types | High performance with idiomatic Kotlin
inline fun <reified T> printTypeName() {
println(T::class.simpleName)
}
fun main() {
printTypeName<String>()
println(String::class.simpleName)
}
@magdamiu
magdamiu / Crossinline.kt
Created December 17, 2021 11:17
crossinline modifier | High performance with idiomatic Kotlin
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 December 17, 2021 10:39
inline and noinline modifiers | High performance with idiomatic Kotlin
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 December 12, 2021 20:18
Lambdas expressions & Closure (capturing lambda) | High performance with idiomatic Kotlin
var priceBooks = 0.0
val prefixPriceDetails = "The current sum is "
books.forEach {
priceBooks += it.price
println("$prefixPriceDetails $priceBooks")
}
@magdamiu
magdamiu / HighOrderFunctions.kt
Created December 12, 2021 12:15
High-order functions | High performance with idiomatic Kotlin
fun titleStartsWithS(book: Book) = book.title.startsWith("S")
fun lengthOfTitleGraterThan5(book: Book) = book.title.length > 5
fun authorStartsWithB(book: Book) = book.author.startsWith("B")
val book1 = Book("Start with why", "Simon Sinek")
val book2 = Book("Dare to lead", "Brene Brown")
val books = listOf(book1, book2)
// this code should be improved
val filteredBooks = books
@magdamiu
magdamiu / PureFunctions.kt
Created December 10, 2021 18:55
Pure functions | High performance with idiomatic Kotlin
class Book(val title: String, val author: String) {
// impure function
fun getBookDetails() = "$title - $author"
}
// pure function
fun getBookDetails(title: String, author: String) = "$title - $author"
@magdamiu
magdamiu / CleanErrorHandling3.kt
Created August 21, 2021 13:11
Clean Code with Kotlin by Magda Miu - Error Handling 2 - Sealed Classes Improved Version - Clean Code
val <T> T.exhaustive: T
get() = this
fun displayMovieResult(movieResult: MovieSearchResult) {
when(movieResult) {
is MovieFound -> println("yey, we found the movie")
is MovieNotFound -> TODO()
}.exhaustive
}
@magdamiu
magdamiu / CleanErrorHandling2.kt
Last active August 21, 2021 13:10
Clean Code with Kotlin by Magda Miu - Error Handling 2 - Sealed Classes - Clean Code
sealed class MovieSearchResult
data class MovieFound(val movie: Movie) : MovieSearchResult()
object MovieNotFound : MovieSearchResult()
object DatabaseOffline : MovieSearchResult()
fun displayMovieResult(movieResult: MovieSearchResult) {
when(movieResult) {
is MovieFound -> println("yey, we found the movie")
is MovieNotFound -> TODO()
}