Skip to content

Instantly share code, notes, and snippets.

@latant
latant / Neo4jQueryTemplates.kt
Last active March 18, 2019 12:36
Template functions for the most common neo4j queries using erictsangx/kotlin-neo4j
import github.etx.neo4j.CursorWrapper
import github.etx.neo4j.NeoQuery
import github.etx.neo4j.destruct
import github.etx.neo4j.DefaultNeoSerializer
val driver: Driver = GraphDatabase.driver(
"bolt://localhost",
AuthTokens.basic("neo4j", "demo")
)
@latant
latant / ApiObject.js
Last active March 29, 2019 18:35
Creating REST API description object in Javascript, then automatically create the methods in the same object.
//Example API description object.
//In most requests we attach data as query params and/or json.
//Usually the fields can be optional in some requests.
const REQ = 'required'
const OPT = 'optional'
export const Api = {
users: {
me: {
GET: {}
},
@latant
latant / call_logging.groovy
Last active April 19, 2019 21:19
Example of logging method calls in groovy with Interceptor and ProxyMetaClass
class DeterminantTeller {
Double tellToQuadratic(Double a, Double b, Double c) { b ** 2 - 4 * a * c }
}
class Solver {
def teller = new DeterminantTeller()
List<Double> solveQuadratic(Double a, Double b, Double c) {
@latant
latant / memoize.kt
Created May 17, 2019 15:55
Memoization in Kotlin
fun <A, R> memoize(function: (A) -> R): (A) -> R {
val results = mutableMapOf<A, R>()
return { arg -> results[arg] ?: function(arg).also { results[arg] = it } }
}
fun fib(n: Long): Long = if (n < 2) n else cfib(n - 1) + cfib(n - 2)
val cfib = memoize(::fib)
@latant
latant / replace_param.kt
Created July 29, 2019 15:34
A function for string templating
fun replaceParams(string: String, params: Map<String, Any>) = buildString {
var i = 0
var j: Int
while (i < string.length) {
while (string[i] != '{') {
append(string[i])
i++
if (i > string.length)
return@buildString
}
(ns clojure-play.core
(:require [quil.core :as q :include-macros true]
[quil.middleware :as m]))
(def world-size 16)
(def cell-padding-ratio 0.1)
(def win-size 500)
(def cell-radius-ratio 0.1)
(def update-millis 100)
@latant
latant / seq.kt
Created November 15, 2019 14:56
Tiny implementation of lazy sequences
class Seq<out T>(val takeOne: () -> Pair<T, Seq<T>>?)
val nil: Seq<Nothing> = Seq { null }
fun <T> cons(head: T, tail: Seq<T> = nil) = Seq { Pair(head, tail) }
fun <T> list(vararg elements: T): Seq<T> {
var result: Seq<T> = nil
for (i in elements.indices.reversed())
result = cons(elements[i], result)
return result
}
tailrec fun <T> Seq<T>.forEach(consume: (T) -> Unit) {
@latant
latant / delegates.kt
Last active October 25, 2020 13:29
Helper interfaces for Kotlin delegates
import kotlin.reflect.KProperty
interface ValDelegate<C, P> {
operator fun getValue(thisRef: C, prop: KProperty<*>): P
}
interface ValDelegateProvider<C, P> {
operator fun provideDelegate(thisRef: C, prop: KProperty<*>): ValDelegate<C, P>
}
@latant
latant / del-fulltext-node-indexes.cypher
Last active February 11, 2020 14:18
Deleting fulltext node indexes from Neo4J database
call db.indexes()
yield indexName, type
with [i in collect({name: indexName, type: type}) where i.type = "node_fulltext" | i.name] as nis
unwind nis as n
call db.index.fulltext.drop(n)
return true
@latant
latant / facebook_your_messages_html_fix.kt
Last active March 10, 2020 14:50
'Download your facebook information' generates html to view your messages, but the href attributes contain wrong directory names.It can be solved quickly by 'lowerCase-ing' the href attributes that start with 'messages' path segment.
import org.jsoup.Jsoup
import java.io.File
fun main(args: Array<String>) {
val (srcFile, targetFile) = args
val doc = Jsoup.parse(File(srcFile).readText())
doc.select("[href]").toList().forEach {
val pathSegments = it.attr("href").split("/").toMutableList()
if (pathSegments[0] == "messages") {
pathSegments[2] = pathSegments[2].toLowerCase()