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 / single-coroutine-executor.kt
Last active June 5, 2019 09:17
The idea of single threaded execution in Kotlin Coroutine.
private val semaphore = AtomicBoolean(false)
private suspend fun Service.checkAndRefreshAccessToken() {
val aSecond = 1000L
  var mills = 1L
  // wait (binary-) semaphore to ensure only one coroutine
// executing this function's try ... catch body.
  while (!semaphore.compareAndSet(false, true)) {
    delay(millis)
    mills = minOf(aSecond, millis * 2)
@ryohji
ryohji / exercise-2.6.md
Last active July 4, 2019 15:44
計算機プログラムの構造と解釈 第二版 問題 2.6

問題 2.6

対を手続きで表現することがそれほどの驚きでなければ,手続きを操作出来る言語では,0 と, 1 を足す演算を

(define zero (lambda (f) (lambda (x) x)))

(define (add-1 n)
 (lambda (f) (lambda (x) (f ((n f) x)))))

と実装することで,(少なくとも非負の整数だけを問題とする限りは)数を使わないで済せることが出来ることを考えよう.この表現は発明者 Alonzo Church(λ算法を発明した論理学者)に従い, Church 数 (Church numerals)として知られている.

@ryohji
ryohji / exercise-2.4.md
Created July 4, 2019 15:46
計算機プログラムの構造と解釈 第二版 問題 2.4

問題 2.4

これは対のもう一つの手続き表現である.この表現について任意のオブジェクト xy に対し, (car (cons x y))x を生じることを証明せよ.

(define (cons x y)
 (lambda (m) (m x y)))

(define (car z)
 (z (lambda (p q) p)))
@ryohji
ryohji / android-paging.diff
Created July 15, 2019 15:22
The diff of google code lab's android-paging.
diff --git a/app/src/main/java/com/example/android/codelabs/paging/data/GithubRepository.kt b/app/src/main/java/com/example/android/codelabs/paging/data/GithubRepository.kt
index b1b9b6c..c23688f 100644
--- a/app/src/main/java/com/example/android/codelabs/paging/data/GithubRepository.kt
+++ b/app/src/main/java/com/example/android/codelabs/paging/data/GithubRepository.kt
@@ -17,9 +17,8 @@
package com.example.android.codelabs.paging.data
import android.util.Log
-import androidx.lifecycle.MutableLiveData
+import androidx.paging.LivePagedListBuilder
@ryohji
ryohji / kotlin-native+coroutine.diff
Created July 27, 2019 15:56
Kotlin/Native プロジェクト(サンプル)に Coroutine を追加する
diff --git a/build.gradle b/build.gradle
index 29bd433..6a89632 100644
--- a/build.gradle
+++ b/build.gradle
@@ -26,6 +26,9 @@ kotlin {
}
macosTest {
}
+ commonMain.dependencies {
+ implementation('org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.2.2')
@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) {
@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 / 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 / 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 / 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()