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 / 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()
interface State
interface Event
data class Transition(val event: Event, val state State)
val transitions: Map<State, Set<Transition>) = ...
@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 } }
@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 / 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 / ImageProcessing.kt
Created December 13, 2019 08:59
2 dimensional image processing (filter)
object ImageProcessing {
fun smoothing(stride: Int, buffer: ByteArray): ByteArray =
buffer.toIntList()
.convolution(stride)
.map { (it / 9).toByte() }
.toByteArray()
fun sharpen(stride: Int, buffer: ByteArray): ByteArray =
buffer.toIntList()
interface Substitution {
fun with(vararg ps: Predicate): Predicate
}
interface Predicate {
fun substitute(vararg xs: Symbol): Substitution
}
data class Symbol(val name: String) : Predicate {
override fun toString(): String = name
@ryohji
ryohji / EqLogic.g4
Created April 18, 2020 14:04
Equational logic syntax
grammar EqLogic;
predicate
: predicate substitution
| LPAREN predicate RPAREN
| BOOLEAN
| ID
| NOT predicate
| predicate IMPLY predicate
| predicate AND predicate
@ryohji
ryohji / apparent-gibberish.md
Last active April 25, 2020 17:43
Make sense of the apparent gibberish in “A Logical Approach to Discrete Math” Ch.0

Every value in array segment b[1..n] that is not in b[i..j] is in b[i..j].

Let predicate p(v) be "v is in b[i..j]."

  (A k: 0<k<=N: ~p(b[k]) -> p(b[k]))
=
  (A k: 0<k<=N: ~~p(b[k]) | p(b[k]))
=
 (A k: 0
@ryohji
ryohji / gist:8a54bb046555d703a316d418203ce401
Created May 2, 2020 18:25
(A ⇒ B) ∧ (¬A ⇒ C) = (A ∧ B) ∨ (¬A ∧ C)
(A ⇒ B) ∧ (¬A ⇒ C)
=
(¬A ∨ B) ∧ (¬¬A ∨ C)
=
(¬A ∨ B) ∧ (A ∨ C)
=
((¬A ∨ B) ∧ A) ∨ ((¬A ∨ B) ∧ C)
=
((¬A ∧ A) ∨ (B ∧ A)) ∨ ((¬A ∧ C) ∨ (B ∧ C))
=