Skip to content

Instantly share code, notes, and snippets.

Alan SCHNEIDER shkschneider

Block or report user

Report or block shkschneider

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@shkschneider
shkschneider / settings.gradle.kts
Last active Sep 4, 2019
The last settings.gradle.kts
View settings.gradle.kts
fun submodules(parent: String? = "", file: File) {
file.listFiles()
?.filter { it.isFile && it.name.endsWith(".gradle") }
?.forEach {
val module = it.parentFile.name
println(":$module")
include(":$module")
if (parent != null) {
project(":$module").projectDir = File(rootDir, "$parent/$module")
}
View Kotlin_Interfaces.kt
@Parcelize
data class User(
override val id: UUID = UUID.randomUUID(),
override val name: String?
) : Parcelable, ScopedUser
interface ScopedUser {
val id: UUID
}
View Kotlin_Mappers.kt
// User.kt
@Parcelize
data class User(
val id: UUID = UUID.randomUUID(),
val name: String?
) : Parcelable
// ScopedUser.kt
@Parcelize
data class ScopedUser(
View Kotlin_Bundles.kt
private const val ID = "id"
private const val NAME = "name"
@Parcelize
data class User(
val id: UUID = UUID.randomUUID(),
val name: String?
) : Parcelable {
fun toBundle() = bundleOf(
View Kotlin_implicit_return4.kt
fun EditText.setOnKeyboardSubmitListener(block: () -> Unit) {
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
block()
true // implicit and won't do anything
// should use return@setOnEditorActionListener
}
false // explicit
}
}
@shkschneider
shkschneider / _Number.kt
Created May 9, 2019
Number.toEnglish()
View _Number.kt
// toEnglish
// <https://stackoverflow.com/a/47376072/603270>
private val zero = "zero"
private val oneToNine = listOf("one", "two", "three", "four", "five", "six", "seven", "height", "nine")
private val tenToNinteen = listOf("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen")
private val dozens = listOf("ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety")
private fun fromOneToAHundred(number: Short): String = if (number == 0.toShort()) "" else
when {
View Kotlin_implicit_return3.kt
fun x(): String = when {
// ...
// ... ->
{
val value = whatever()
if (value) {
"something"
} else {
""
}
View _Enum.kt
// <https://github.com/tailwhiper/kotlin-enum-extensions>
inline fun <reified E : Enum<E>> valueOf(name: String, default: E) =
enumValues<E>().find { it.name == name } ?: default
inline fun <reified E : Enum<E>> valueOf(ordinal: Int, default: E) =
enumValues<E>().find { it.ordinal == ordinal } ?: default
inline fun <reified E : Enum<E>> valueOfWithCondition(condition: (E) -> Boolean) =
enumValues<E>().find(condition)
View Kotlin_implicit_return2.kt
.map {
with(createRequest(Type.NEXT, it, data)) {
val keywords = // ...
copy(keywords = keywords)
}
}
View Kotlin_implicit_type.kt
fun returnsUnit() {
methodReturningBoolean()
}
fun returnsAnImplicitBoolean() = methodReturningBoolean()
private fun methodReturningBoolean() = true
You can’t perform that action at this time.