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.io.Serializable | |
import java.rmi.Remote | |
import java.rmi.RemoteException | |
import java.rmi.registry.LocateRegistry | |
import java.rmi.server.UnicastRemoteObject | |
class Person(val name: String, val age: Int): Serializable | |
interface HelloService : Remote { | |
@Throws(RemoteException::class) |
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
//In Kotlin, we can't create 'var' extension property that requires backing field. (https://kotlinlang.org/docs/reference/extensions.html#extension-properties) | |
//However, in Kotlin JS, we can can 'create' backing fields, due to the JS Object's dynamic nature. | |
var HTMLElement.myProp: String | |
get() = this.asDynamic().myProp | |
set(value) { this.asDynamic().myProp = value } | |
//We can also create custom property delegate to remove boilerplate: | |
class DynamicProperty<T> { | |
operator fun getValue(thisRef: Any, prop: KProperty<*>) = thisRef.asDynamic()[prop.name] |
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 core | |
open class PList<T>(val value: T, val next: PList<T>?) : Iterable<T> { | |
override fun iterator() = object : Iterator<T> { | |
var PList: PList<T>? = this@PList | |
override fun hasNext() = PList != null | |
override fun next() = PList!!.value.also { PList = PList!!.next } | |
} |
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
interface NioSocketServer { | |
fun stop() | |
suspend fun SelectableChannel.awaitSelection(ops: Int): SelectionKey | |
} | |
fun nioSocketServer(port: Int, acceptor: suspend NioSocketServer.(SocketChannel) -> Unit) { | |
var isStopped = false | |
val serverChannel = ServerSocketChannel.open().apply { | |
configureBlocking(false) | |
bind(InetSocketAddress(port)) |
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
(defmulti then-fn | |
(fn [deferred _] (class deferred))) | |
(defmethod then-fn CompletableFuture [^CompletableFuture future func] | |
(.thenAccept future | |
(reify Consumer | |
(accept [_ result] (func result))))) | |
(defmacro async [arg & block] | |
(if (vector? arg) |
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 PrettyStringCreation: | |
def pstr_list(self, l: list, opts: dict): | |
plist = [] | |
for e in l: | |
plist.append(self.pstr(e, opts)) | |
return '[' + ', '.join(plist) + ']' | |
def pstr_dict(self, d: dict, opts: dict): | |
sl = [] | |
for k in d.keys(): |
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
call { | |
match p=()--()--() return | |
reduce(ns = [], n in collect(nodes(p)) | ns + n) as ns, | |
reduce(rs = [], r in collect(relationships(p)) | rs + r) as rs | |
} | |
call { with ns unwind ns as n return collect(distinct n) as nodes } | |
call { with rs unwind rs as r return collect(distinct r) as relationships } | |
return {nodes: nodes, relationships: relationships} |
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 kotlin.concurrent.thread | |
import kotlin.coroutines.resume | |
import kotlin.coroutines.suspendCoroutine | |
suspend fun awaitShutdown() { | |
suspendCoroutine<Unit> { c -> | |
Runtime.getRuntime().addShutdownHook(thread(start = false) { | |
c.resume(Unit) | |
}) | |
} |
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 org.pcollections.PSet | |
fun <T> PSet<T>.permutations(): Sequence<Sequence<T>> = when { | |
size < 2 -> sequenceOf(asSequence()) | |
else -> asSequence().flatMap { x -> minus(x).permutations().asSequence().map { sequenceOf(x) + it } } | |
} |
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
// Loop-recur enables recursive programming without the need of defining a recursive function | |
fun <A, R> loop(a: A, action: Loop1<A, R>.(A) -> R) = Loop1(action).recur(a) | |
class Loop1<A, R>(private val action: Loop1<A, R>.(A) -> R) { | |
fun recur(a: A) = action(a) | |
} | |
fun main() { | |
val n = readLine()!!.toInt() |