Skip to content

Instantly share code, notes, and snippets.

Avatar
🦎

Sergey Mashkov cy6erGn0m

🦎
View GitHub Profile
@cy6erGn0m
cy6erGn0m / file-sync.kt
Last active Aug 29, 2015
Naive file sync implementation in Kotlin
View file-sync.kt
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)
@cy6erGn0m
cy6erGn0m / kotlin-websocket-example.kt
Last active Apr 25, 2018
This example demonstrates how to use WebSocket including reconnect when needed and error handling
View kotlin-websocket-example.kt
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 {
@cy6erGn0m
cy6erGn0m / merge-maps.kt
Created May 20, 2015
Merge two maps with custom reduce function for Kotlin
View merge-maps.kt
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 {
@cy6erGn0m
cy6erGn0m / multi-declarations-with-regex.kt
Last active Aug 29, 2015
Demostrates Power of Kotlin's multi-declarations
View multi-declarations-with-regex.kt
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")
View listeners.kt
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}")}
}
View test.kt
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)
View traitOverrideProperty.kt
trait Test {
val test : String
}
class TestImpl (test : String) : Test {
override val test = test
}
@cy6erGn0m
cy6erGn0m / recursion.kt
Created Oct 14, 2013
inner function annotation problem example
View recursion.kt
TailRecursive fun g2() {
TailRecursive fun g3(counter : Int, x : Any) : Unit {
if (counter > 0) { g3(counter - 1, "tail 97") }
}
g3(1000000, "test")
}
@cy6erGn0m
cy6erGn0m / recursion1.kt
Created Oct 13, 2013
Kotlin tail recursive foldl demo
View recursion1.kt
fun <T, A> Iterator<T>.foldl(acc : A, foldFunction : (e : T, acc : A) -> A) : A =
if (!hasNext()) acc
else foldl(foldFunction(next(), acc), foldFunction)
val sum = (1..1000000).iterator().foldl(0) { (e : Int, acc : Long) ->
acc + e
}
@cy6erGn0m
cy6erGn0m / myFiles.erl
Created Oct 2, 2013
Yet another directory recursive delete function for Erlang
View myFiles.erl
ensureDirSlash(Dir, Child) ->
case lists:suffix("/", Dir) of
true -> Dir ++ Child;
false -> Dir ++ "/" ++ Child
end.
-spec deleteDir(Dir :: nonempty_string()) -> none().
deleteDir(Dir) ->
case {filelib:is_dir(Dir), isFileOrLink(Dir)} of
{_, true} -> throw("The specified path is not a directory: " ++ Dir);