Skip to content

Instantly share code, notes, and snippets.

View ryohji's full-sized avatar

Ryohji Ikebe ryohji

  • Tokyo, Japan
View GitHub Profile
@ryohji
ryohji / converter.kt
Last active December 13, 2019 11:21
Convert `YUV_420_888` format image (from Android Camera2 API) into Jpeg ByteArray (can be used with BitmapFactory.decodeByteArray)
import android.graphics.ImageFormat.NV21
import android.graphics.Rect
import android.graphics.YuvImage
import android.media.Image
import java.io.ByteArrayOutputStream
object Converter {
fun jpegByteArrayFrom(yuv420_888: Image): ByteArray =
yuv420_888.nv21ByteArray
@ryohji
ryohji / partial-recursive-function.md
Created December 12, 2019 11:01
Partial recursive function (#1 to #3) (from "アンダースタンディング コンピュテーション")

P.211 ~ 212 の部分再帰関数の実装。名前づけで混乱したので整理しなおしてみた。

# 部品1
def zero
    0
end

# 部品2
def increment(n)
 n + 1
@ryohji
ryohji / direct-product.kt
Last active November 28, 2019 12:04
Direct product in Kotlin
fun <E> Collection<Collection<E>>.directProduct(): List<List<E>> =
if (isEmpty()) {
listOf()
} else {
val (head, rest) = first() to drop(1)
head.map(::listOf).let {
if (rest.isEmpty()) {
it
} else {
rest.directProduct().flatMap { tail -> it.map { h -> h + tail } }
interface State
interface Event
data class Transition(val event: Event, val state State)
val transitions: Map<State, Set<Transition>) = ...
@ryohji
ryohji / prod-cons-1.md
Last active October 27, 2019 17:06
Producer/Consumer model (without Conditional variable)
val mutex = java.util.concurrent.locks.ReentrantLock()

const val FULL = 2
var que = 0

fun prod() {
    while (true) {
        // P0: lock
 mutex.lock()
@ryohji
ryohji / ddsv.kt
Created October 19, 2019 17:38
Deadlock detector first implementation.
package app
fun main() {
val rules: List<Rule> = listOf(
object : Rule {
override val name = "read"
override fun applicable(shared: Shared, thread: Thread) = thread.ir == Location.P0
override fun convert(shared: Shared, thread: Thread) = shared to Local(Location.P1, shared.x)
},
object : Rule {
@ryohji
ryohji / PagedList.md
Last active June 15, 2020 04:36
ListView と RecyclerView の比較、そして PagedList の紹介

ListView

ListView

ListView は、その表示するリスト項目の内容を保持する(そしてその内容を表示するためのビューを返す) ListAdapter をつなぐことで動作する。

class Adapter : BaseAdapter() {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View =
@ryohji
ryohji / Executor.kt
Last active October 9, 2019 16:43
Executor implementation for PagedList.Builder's setFetchExecutor and setNotifyExecutor.
object Executor {
val uiExecutor = Handler(Looper.getMainLooper()).toExecutor()
val ioExecutor = LooperThread().also { it.start() }.getHandler().toExecutor()
private fun Handler.toExecutor(): Executor = Executor {
while (!post(it)) {
Thread.sleep(0)
}
}
@ryohji
ryohji / factorial.js
Created September 12, 2019 15:46
Z combinator
const Z = f => (x => f(y => x(x)(y)))(x => f(y => x(x)(y)));
const fact = Z(f => x => x != 0 ? x * f(x-1) : 1);
fact(5); // => 120
@ryohji
ryohji / factorial.C
Created September 12, 2019 15:29
Z combinator
#include <cstdio>
int main() {
auto z = [](auto f) {
auto g = [&f](auto x) {
return f([&x](auto y) { return x(x)(y); });
};
return g(g);
};
auto f = [](auto f) {