Skip to content

Instantly share code, notes, and snippets.

@latant
latant / curry.ts
Created October 13, 2022 06:15
Function currying type in TypeScript
type Curry<F> = F extends (arg: infer A, ...args: infer Rest) => infer R
? (arg: A) => Rest extends [any] ? Curry<(...args: Rest) => R> : R
: never;
@latant
latant / neoql.ts
Last active February 25, 2022 15:14
Creating graphql-like type-safe construct using the advanced types of TypeScript
type PropertyType = "string" | "number" | "date";
type LinkSchema = { incoming: string } | { outgoing: string };
type PropertySchema =
| { nullable: PropertyType | [PropertyType] }
| { required: PropertyType | [PropertyType] };
type NodeRefSchema<L> = ({ nullable: L } | { required: L | [L] }) & LinkSchema;
type FieldSchema<L> = PropertySchema | NodeRefSchema<L>;
type NodeSchema<L> = { [key: string]: FieldSchema<L> };
type GraphSchema<L> = { [key: string]: NodeSchema<L> };
@latant
latant / the-mind.clj
Last active October 24, 2021 12:59
Calculation on the possibility that there is two dealt card with the distance of 1 in the card game called 'The MInd'.
(def nnv
(memoize
(fn [n k]
(cond
(= k 0) 1
(> (* 2 k) (+ 1 n)) 0
(= (* 2 k) (+ 1 n)) 1
:else (bigint
(+ (nnv (dec n) k)
(nnv (dec (dec n)) (dec k))))))))
@latant
latant / async-await.clj
Last active October 20, 2021 21:56
Clojure async/await for CompletableFuture using cloroutine. It works with thread bindings.
(ns async
(:require [cloroutine.core])
(:import (java.util.concurrent CompletableFuture)
(java.util.function BiConsumer)
(clojure.lang Var)))
(def ^:dynamic *coroutine*)
(def ^:dynamic *value*)
(def ^:dynamic *error*)
@latant
latant / keybindings.json
Created May 4, 2021 08:07
VSCode keybindings settings for rapid Prolog programming
[
{
"when": "editorTextFocus && editorLangId == prolog",
"key": "alt+enter",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"workbench.action.files.save",
"workbench.action.terminal.focus",
{
@latant
latant / colex.clj
Created March 28, 2021 21:21
Colexicographic order in Clojure
(defn first-index [f coll]
(->> (map-indexed vector coll)
(filter (comp f second))
(map first)
(first)))
(defn first-incable [v]
(or
(first-index
(fn [[a b]] (< (inc a) b))
@latant
latant / neo-plugin.Dockerfile
Created December 16, 2020 15:37
Dockerfile for Neo4j plugin deployment.
FROM gradle:6.7.1-jdk11 as build
COPY . /neo4j-plugin
WORKDIR /neo4j-plugin
RUN gradle shadowJar
FROM neo4j:4.2
COPY --from=build /neo4j-plugin/build/libs/*-all.jar /plugins/
ENV NEO4J_dbms_security_procedures_allowlist=example.*
@latant
latant / neo-plugin-dev.Dockerfile
Last active December 16, 2020 15:38
A Dockerfile for Neo4j plugin development with Gradle using caches for fast builds.
FROM gradle:6.7.1-jdk11 AS deps
ENV GRADLE_USER_HOME=/gradle-home
ADD ./settings.gradle.kts /neo4j-plugin/settings.gradle.kts
ADD ./build.gradle.kts /neo4j-plugin/build.gradle.kts
WORKDIR /neo4j-plugin
RUN gradle build
RUN ls -a /gradle-home
FROM gradle:6.7.1-jdk11 as build
ENV GRADLE_USER_HOME=/gradle-home
@latant
latant / looprecur.kt
Created November 23, 2020 19:04
Loop-recur in Kotlin
// 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()
@latant
latant / permutations.kt
Created November 7, 2020 17:55
efficient permutations in Kotlin
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 } }
}