Skip to content

Instantly share code, notes, and snippets.

Avatar
🦎

Sergey Mashkov cy6erGn0m

🦎
View GitHub Profile
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 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}")}
}
@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")
@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 / Main.java
Created Jun 8, 2012
Simplest HTTP request example
View Main.java
package cg;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author Sergey Mashkov aka cy6erGn0m
* @since 08.06.12
@cy6erGn0m
cy6erGn0m / Main.java
Created Jun 21, 2012
Simple example jansi library
View Main.java
package cg;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiConsole;
/**
* User: Sergey Mashkov
* Date: 6/21/12
*/
public class Main {
@cy6erGn0m
cy6erGn0m / test.groovy
Created Aug 17, 2012
Groovy dot "override"
View test.groovy
def map = [:]
map.test = 1
println map.test
map.xxx = [ aa : 'aa']
println map.xxx.aa
@cy6erGn0m
cy6erGn0m / pairs-sequence.kt
Created Nov 12, 2015
Returns a sequence that consists of all possible pair of original list elements, does nothing with potential duplicates
View pairs-sequence.kt
/**
* 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)
}
@cy6erGn0m
cy6erGn0m / draganddrop.kt
Created Dec 1, 2015
Drag and drop example
View draganddrop.kt
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
@cy6erGn0m
cy6erGn0m / JavaRegexpTest.java
Created Mar 3, 2013
Regexp performance difference example
View JavaRegexpTest.java
package cg;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**