Skip to content

Instantly share code, notes, and snippets.

View pablisco's full-sized avatar
🤔
Is this Slack?

Pablisco pablisco

🤔
Is this Slack?
View GitHub Profile
@pablisco
pablisco / Behaviour.md
Last active February 1, 2024 15:49
Query Behaviour

Behaviour

One of the things we tend to do in OOP is to create types to define behaviours. However, types are best used to describe data, i.e. objects. We've had to use types in the past because of language limitations.

This is a small description of how we can use the power of functions to define code in a way that is easier to read, maintain and test.

Consumer Component

@pablisco
pablisco / Coroutine_scope_functions.kt
Last active January 11, 2024 16:42
Simple way to jump into a separate context
suspend fun <T, R> T.letOn(
context: CoroutineContext,
block: suspend CoroutineScope.(T) -> R
): R = withContext(context) { block(this@letOn) }
suspend fun <T> T.alsoOn(
context: CoroutineContext,
block: suspend CoroutineScope.(T) -> Unit
): T = also { withContext(context) { block(this@alsoOn) } }
@pablisco
pablisco / gist:da25563d57559dd1d18f165272269b57
Last active April 15, 2022 03:23
ResourcesExceptions.kt
import android.content.Context
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.support.annotation.AnyRes
import android.support.v4.app.Fragment
import android.support.v4.content.res.ResourcesCompat.*
import android.view.View
val Context.animations
get() = ResourceMapper { resources.getAnimation(it) }
@pablisco
pablisco / README.md
Last active September 7, 2020 11:31
Gradle Multi project settings Kotlin DSL

Gradle Multi project settings Kotlin DSL

This is a small Gradle DSL that can be used to include multiple modules. Let's imagine that we have a set up like this:

root
+-- app
+-- modules
    +-- core
 +-- androidx
public Object evaluate(float fraction, Object startValue, Object endValue) {
int startInt = (Integer) startValue;
float startA = ((startInt >> 24) & 0xff) / 255.0f;
float startR = ((startInt >> 16) & 0xff) / 255.0f;
float startG = ((startInt >> 8) & 0xff) / 255.0f;
float startB = ( startInt & 0xff) / 255.0f;
int endInt = (Integer) endValue;
float endA = ((endInt >> 24) & 0xff) / 255.0f;
float endR = ((endInt >> 16) & 0xff) / 255.0f;
@pablisco
pablisco / ReflexionExtensions.kt
Created September 3, 2017 23:54
Port of Gson's TypeToken to Kotlin
import java.io.Serializable
import java.lang.reflect.*
import java.lang.reflect.Array as ArrayType
import java.util.*
val EMPTY_TYPE_ARRAY: kotlin.Array<Type> = emptyArray()
/**
* Returns a new parameterized type, applying {@code typeArguments} to
* {@code rawType} and enclosed by {@code ownerType}.
@pablisco
pablisco / Creating subjects in tests with private constructors
Last active September 7, 2018 09:35
Creating subjects in tests with private constructors
Left empty
package com.pusher.chatkit.users
val string by lazy { "" }
val List<String>.describe: String by lazy {
"this"
}
val List<String>.something: String get() {
return ""
/**
* Used to do a trailing throttle of actions.
*/
internal class Throttler<A, B>(
private val delay: Long = 500,
private val action: (A) -> B
) {
private val target = AtomicReference<A>()
@pablisco
pablisco / Logger.kt
Last active May 17, 2018 10:35
Fluent Logging with Kotlin
inline fun <A> A.logWith(logger: Logger, block: Logger.(A) -> Unit) : A =
this.also { logger.block(it) }
// With interface injection
interface HasLog {
val log: Logger
fun <A> A.log(block: Logger.(A) -> Unit) : A =
logWith(logger, block)
}