Skip to content

Instantly share code, notes, and snippets.

View chao2zhang's full-sized avatar
:octocat:

Chao Zhang chao2zhang

:octocat:
View GitHub Profile
class R;
#include "rgbapixel.h"
/**
* This function constructs a default pixel which is opaque
* (non-transparent) and white.
**/
RGBAPixel::RGBAPixel()
{
alpha = 255;
red = 255;
@chao2zhang
chao2zhang / Twitter (json format).js
Created October 20, 2015 13:28 — forked from gnip/Twitter (json format).js
Twitter Sample Payload, JSON format
{
"coordinates": null,
"created_at": "Thu Oct 21 16:02:46 +0000 2010",
"favorited": false,
"truncated": false,
"id_str": "28039652140",
"entities": {
"urls": [
{
"expanded_url": null,
@chao2zhang
chao2zhang / ObserveUntilFinished.kt
Created October 27, 2019 19:45
ObserveUntilFinished example
fun <T> observeUntilFinished(liveData: LiveData<T>, observer: Observer<T>? = null) {
liveData.observeForever(object : Observer<T> {
override fun onChanged(resource: Resource<T>?) {
observer?.onChanged(resource)
liveData.removeObserver(this) // SAM conversion is not applicable because of `this`
}
})
}
@chao2zhang
chao2zhang / JavaSam.java
Created October 27, 2019 19:55
JavaSam.java
@FunctionalInterface
public interface JavaSam {
boolean apply(@NotNull Integer id);
}
// JavaApp.java
private boolean isEven(int id) {
return id % 2 == 0;
}
public boolean invokeJavaSam(int id, JavaSam sam) {
return sam.apply(id);
}
@chao2zhang
chao2zhang / JavaApp.java
Last active November 27, 2019 20:32
JavaCode consuming Kotlin Function
// KotlinApp.kt
typealias KotlinFunction = (Int) -> Boolean
fun invokeKotlinFunction(id: Int, f: KotlinFunction) = f(id)
// JavaApp.java
private void useKotlinFunction()
Function1<Integer, Boolean> kotlinFunc = id -> id % 2 == 0;
KotlinAppKt.invokeKotlinFunction(3, id -> id % 2 == 0);
KotlinAppKt.invokeKotlinFunction(4, this::isEven);
@chao2zhang
chao2zhang / KotlinApp.kt
Last active November 26, 2019 07:07
KotlinApp.kt : Kotlin -> Java
fun isEven(id: Int) = id % 2 == 0
fun invokeJavaSam(id: Int, sam: JavaSam) = sam.apply(id)
fun useJavaSam() {
val javaSam = JavaSam { id -> id % 2 == 0 }
invokeJavaSam(3, JavaSam { id -> id % 2 == 0 })
invokeJavaSam(4, JavaSam { isEven(it) })
// public boolean invokeJavaSam(int id, JavaSam sam);
@chao2zhang
chao2zhang / KotlinApp.kt
Last active November 26, 2019 06:59
KotlinApp.kt : Kotlin -> Kotlin
fun invokeKotlinInterface(id: Int, i: KotlinInterface) = i.apply(id)
fun useKotlinInterface() {
val kotlinInterface = object : KotlinInterface {
override fun apply(id: Int) = id % 2 == 0
}
invokeKotlinInterface(3, object : KotlinInterface {
override fun apply(id: Int) = id % 2 == 0
})
invokeKotlinInterface(4, object : KotlinInterface {
@chao2zhang
chao2zhang / KotlinApp.kt
Last active November 26, 2019 06:59
KotlinApp.kt : Kotlin -> Kotlin
fun invokeKotlinFunction(id: Int, f: KotlinFunction) = f(id)
fun useKotlinFunction() {
val kotlinFunction : KotlinFunction = { id -> id % 2 == 0 }
invokeKotlinFunction(3) { id -> id % 2 == 0 }
invokeKotlinFunction(4) { isEven(it) }
invokeKotlinFunction(4, ::isEven)
}