View RecyclerViewClickAndLongClick.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
View CollectionsVsSequences.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
View SealedClass.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
View StringTemplates.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val displayContentWithTemplate = "Hello $firstName! Please confirm that $email is your email address." |
View UseFunction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inputStream.use { | |
outputStream.use { | |
// do something with the streams | |
outputStream.write(inputStream.read()) | |
} | |
} | |
// improved option | |
arrayOf(inputStream, outputStream).use { | |
// do something with the streams |
View Immutability.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
smallList() | |
smallSequence() | |
} | |
fun smallList() = (0..5) | |
.filter { print("list filter($it) "); it % 2 == 0 } | |
.map { print("list map($it) "); it * it } | |
.first() |
View ReifiedTypes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T> printTypeName() { | |
println(T::class.simpleName) | |
} | |
fun main() { | |
printTypeName<String>() | |
println(String::class.simpleName) | |
} |
View Crossinline.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <T, R : Comparable<R>> Iterable<T>.sortedBy(crossinline selector: (T) -> R?): List<T> { | |
return sortedWith(compareBy(selector)) | |
} |
View InlineFunctions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
NewerOlder