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
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) | |
} |
/** | |
* 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>() |
package com.pusher.chatkit.users | |
val string by lazy { "" } | |
val List<String>.describe: String by lazy { | |
"this" | |
} | |
val List<String>.something: String get() { | |
return "" |
Left empty |
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}. |
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; |
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) } |
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) } } |
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.