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;