This file contains 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 dnd | |
import java.awt.* | |
import java.awt.dnd.* | |
import javax.swing.* | |
fun main(args: Array<String>) { | |
SwingUtilities.invokeAndWait { | |
val f = JFrame("test") | |
f.defaultCloseOperation = JFrame.EXIT_ON_CLOSE |
This file contains 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
/** | |
* Returns a sequence that consists of all possible pair of original list elements, does nothing with potential duplicates | |
* @param skipSamePairs indicates whether it should produce pairs from the same element at both first and second positions | |
*/ | |
fun <T> List<T>.allPairs(skipSamePairs: Boolean = true): Sequence<Pair<T, T>> = PairsSequence(this, skipSamePairs) | |
private class PairsSequence<T>(val source: List<T>, val skipSamePairs: Boolean) : Sequence<Pair<T, T>> { | |
override fun iterator(): Iterator<Pair<T, T>> = PairsIterator(source, skipSamePairs) | |
} |
This file contains 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 sync(from: File, to: File, detectChange: (File, File) -> Boolean, filePredicate : (File) -> Boolean = {true}) { | |
require(from.isDirectory) | |
require(to.isDirectory) | |
from.walkTopDown() | |
.filter { it.isDirectory || filePredicate(it) } | |
.asSequence() | |
.forEach { file -> | |
val relative = file.relativeTo(from) | |
val destFile = File(to, relative) |
This file contains 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.w3c.dom.MessageEvent | |
import org.w3c.dom.WebSocket | |
import kotlin.browser.window | |
class KWebSocketImpl(val url : String, val reconnectDelayMillis: Int, val listener : (dynamic) -> Unit) { | |
private var currentSocket : WebSocket? = null | |
private val queue = arrayListOf<String>() | |
private var closed = false | |
init { |
This file contains 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
private fun <K, V> Map<K, V>.mergeReduce(other: Map<K, V>, reduce: (V, V) -> V = { a, b -> b }): Map<K, V> { | |
val result = LinkedHashMap<K, V>(this.size() + other.size()) | |
result.putAll(this) | |
other.forEach { e -> | |
val existing = result[e.key] | |
if (existing == null) { | |
result[e.key] = e.value | |
} | |
else { |
This file contains 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.util.* | |
import java.util.regex.Pattern | |
fun main(args: Array<String>) { | |
x("http://localhost:9090") | |
x("http://localhost") | |
x("http://localhost/path/to/it") | |
x("http://localhost:9091/path/to/it") | |
x("http://localhost:9091/?p=1") |
This file contains 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
val listener = eventBus { | |
on (javaClass<Started>()) signal { c -> println("job ${c.job} started")} | |
on (javaClass<Failed>()) signal { f -> println("job failed: ${f.t.getMessage()}")} | |
on (javaClass<Completed<Int>>()) signal { c -> println("job ${c.job} completed, result is ${c.result}")} | |
} | |
This file contains 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.util.LinkedList | |
import java.util.Deque | |
import java.util.ArrayList | |
data class TreeNode<T>(val value : T, val children : List<TreeNode<T>> = listOf()) | |
public fun <T, Acc> treeFoldBFS(root : TreeNode<T>, initialValue : Acc, foldFunction : (Acc, T) -> Acc) : Acc = | |
treeFoldImpl(root, LinkedList(), {q -> q.removeLast()}, initialValue, foldFunction) |
This file contains 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
trait Test { | |
val test : String | |
} | |
class TestImpl (test : String) : Test { | |
override val test = test | |
} |
This file contains 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
TailRecursive fun g2() { | |
TailRecursive fun g3(counter : Int, x : Any) : Unit { | |
if (counter > 0) { g3(counter - 1, "tail 97") } | |
} | |
g3(1000000, "test") | |
} |