Skip to content

Instantly share code, notes, and snippets.

@shkschneider
shkschneider / Base58.kt
Created Feb 28, 2020
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
View Base58.kt
package me.shkschneider
// https://gist.github.com/vrotaru/1753908
object Base58 {
private val alphabet = (('0'..'9') - '0' + ('A'..'Z') - 'I' - 'O' + ('a'..'z') - 'l').sorted()
private val base58 = alphabet.size.also {
if (it != 58) throw RuntimeException("Base58!=Base$it")
}
private const val base256 = 256
@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)
}
}
You can’t perform that action at this time.