Skip to content

Instantly share code, notes, and snippets.

View enshahar's full-sized avatar

Frank Hyunsok Oh enshahar

View GitHub Profile
@enshahar
enshahar / Array2D.kt
Created March 27, 2024 12:25
Array2d in Kotlin
class Array2D<T>(val n:Int, val m:Int, private val arr:Array<T>) {
init {
require(n*m == arr.size)
}
companion object {
inline fun <reified T> array2d(n:Int, m:Int, v:T) =
Array2D<T>(n,m,Array<T>(n*m){v})
inline fun <reified T> array2d(n:Int, m:Int, block:(Int,Int)->T) =
Array2D<T>(n,m,Array<T>(n*m){ i ->
package learningtest
import kotlinx.coroutines.ExecutorCoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import kotlin.coroutines.*
object WithoutKotlinx3 {
var saved: MutableList<Any> = mutableListOf()
@enshahar
enshahar / KotlinCoroutineLaunchTest.kt
Created December 1, 2023 01:01
1st implementation of launch (failed)
package learningtest
import kotlin.coroutines.*
private class MyCont<T>(override val context: CoroutineContext): Continuation<T> {
override fun resumeWith(result: Result<T>) {
println("Resume with: ${result}")
}
}
@enshahar
enshahar / ForExample.kts
Last active June 1, 2023 00:34
Iterable, Iterator, operator iterator(), operator hasNext(), operator next()와 for 루프
class MyIterable(private val n:Int): Iterable<Int> {
override fun iterator(): Iterator<Int> = MyIterator(n)
}
class MyIterator(private val max: Int): Iterator<Int> {
private var n:Int=0
override fun next():Int = n
override fun hasNext(): Boolean = n++ < max
}
@enshahar
enshahar / PrivateOrConstructorArgGeneric.kt
Created May 24, 2023 14:11
private 멤버나 constructor는 공변성 검증 대상이 아님
class Box<out T, FUN: ()->T>(v: T, private val nextval: FUN) {
private var value: T = v
private fun log(v: T) {
println(v)
}
fun logPublic(v: T) { // Type parameter T is declared as 'out' but occurs in 'in' position in type T
println(v)
@enshahar
enshahar / FixInKotlin.kt
Created June 24, 2020 10:54
Fixpoint operator
fun FIX(f: ((Int)->Int)->(Int)->Int): (Int)->Int = { x -> f (FIX(f)) (x) }
val fact_ = { fact: (Int)->Int -> { i: Int -> if(i == 0) 1 else (i * fact (i-1)) } }
val fact = FIX(fact_)
println("factorial(10) = ${fact(10)}")
@enshahar
enshahar / FoldAndReduce.kt
Last active June 19, 2020 11:55
Comparing fold and reduce
class FoldAndReduce {
val appendSC = { s: String, c: Char -> s + c }
val appendCS = { c: Char, s: String -> c + s }
val add = { x: Int, y: Int -> x + y }
val sub = { x: Int, y: Int -> x + y }
val cons = { x: Int, y: List<Int> -> listOf(x, *y.toTypedArray()) }
val appendList = { y: List<Int>, x: Int -> y + x }
val list1 = listOf(1, 2, 3, 4, 5)
val list1rev = list1.reversed()
@enshahar
enshahar / SequenceToList.kt
Last active April 10, 2020 07:02
원소 100만개 짜리 시퀀스도 리스트로 못 바꾸는 안습한 코틀린 이라고 생각한 안습한 밥팅 나
import java.lang.Runtime.getRuntime
fun main() {
println("max: ${getRuntime().maxMemory()}, total: ${getRuntime().totalMemory()}, free: ${getRuntime().freeMemory()}")
// 여기서 100_0000는 원소 갯수가 아니고, 시드값임.
// 두번째로 오는 람다에서 널을 반환하면 시퀀스가 끝남
// 따라서 나라는 밥팅은 100_0000이 들어있는 무한 시퀀스를 만든 것임.
val seq1 = generateSequence(100_0000) { it }
val list1 = seq1.toList()
@enshahar
enshahar / gauss.
Created March 11, 2020 13:04 — forked from MatlabMaster/gauss.
Gauss_elimination
## module gaussElimin
''' x = gaussElimin(a,b).
Solve[a]{b} = {x} by gauss elimination.
'''
import numpy as np
def gaussElimin(a,b):
n = len(b)
#Elmination Phase
@enshahar
enshahar / solution.js
Created February 22, 2020 04:48 — forked from indongyoo/solution.js
김동현님 질문 코드
function* filter(f, iter) {
for (const a of iter) if (f(a)) yield a;
}
function* take(limit, iter) {
for (const a of iter) if (limit--) yield a;
}
const head = iter => take(1, iter).next().value;