This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
NewerOlder