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
fun Int.dp(): Int = | |
toFloat().dp().toInt() | |
fun Float.dp(): Float = applyDimension( | |
COMPLEX_UNIT_DIP, | |
this, | |
Resources.getSystem().displayMetrics | |
) |
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) | |
} |
fun <A> A.toJson(): String { | |
fun <A> Iterable<A>.toJsonArray(): String = | |
joinToString(", ", "[", "]") { it.toJson() } | |
fun <A, B> Map<A, B>.toJsonObject() = | |
entries.joinToString(", ", "{", "}") { (key, value) -> "\"$key\" : ${value.toJson()}" } | |
return when(this) { | |
null -> "null" |
/** | |
* 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 |
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) } } |
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; |
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.