Skip to content

Instantly share code, notes, and snippets.

View magdamiu's full-sized avatar

Magda Miu magdamiu

View GitHub Profile
@magdamiu
magdamiu / RecyclerViewClickAndLongClick.java
Created May 20, 2023 11:02
RecyclerView Click and LongClick Handling
// STEP 1 - create new file
public interface ClickListener {
public void onClick(View view, int position);
public void onLongClick(View view, int position);
}
// STEP 2 - create new file
import android.content.Context;
@magdamiu
magdamiu / CollectionsVsSequences.kt
Created December 17, 2021 14:54
Collections vs Sequences
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 December 17, 2021 14:48
Sealed class in Kotlin
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 December 17, 2021 13:56
String templates | High performance with idiomatic Kotlin
val displayContentWithTemplate = "Hello $firstName! Please confirm that $email is your email address."
@magdamiu
magdamiu / UseFunction.kt
Created December 17, 2021 13:50
Disposable pattern to avoid resource leaks | High performance with idiomatic Kotlin
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 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;