Skip to content

Instantly share code, notes, and snippets.

View lgessler's full-sized avatar

Luke Gessler lgessler

View GitHub Profile
@lgessler
lgessler / deftx.clj
Last active May 6, 2021 06:42
Crux transaction function-backed mutations to avoid race conditions
(ns foo (:require [crux.api :as crux]))
(defmacro deftx [name bindings & body]
"Defines a function used for mutations that uses a Crux transaction function under the hood.
body must return a valid Crux transaction vector, with any non-clojure.core variables fully
qualified. `install-tx-fns` must be called on namespaces where deftx is used for the function
to work."
(let [kwd-name (keyword (str *ns*) (str name))
symbol-name (symbol (str name))]
`(def
@lgessler
lgessler / txfn_macro.clj
Last active September 24, 2020 14:24
Macro for crux tx-fns
(defmacro deftx [name node tx-fn]
"Defines a transaction function on the given node."
(let [kwd-name (keyword (str *ns*) (str name))]
`(do
(crux/submit-tx ~node [[:crux.tx/put {:crux.db/id ~kwd-name
:crux.db/fn (quote ~tx-fn)}]])
(defn ~name [& ~'args]
(crux/submit-tx ~node (log/spy [(into [:crux.tx/fn ~kwd-name] ~'args)]))))))
;; without macro
// print 10 x's per second
function printXs(totalMs) {
let begin = null;
let printed = 0;
function callback(tick) {
// note begin time
if (begin === null) {
begin = tick;
}
@lgessler
lgessler / instaconllu.clj
Created January 27, 2020 00:29
instaparse parser for CONLLU format
(ns erracle.conll.parse
(:require [clojure.java.io :as io]
[instaparse.core :as insta]))
(def conllu-parser
(insta/parser
;; This is a parser for the CONLL-U format. Only the vanilla 10 columns are supported.
;; This aims to be complete for the specification as it existed in January 2020.
;; The result is this parser is a seq of 11-item vectors, the first being a keyword
;; indicating the line's type, and the remaining columns containing the CONLLU data.
@lgessler
lgessler / defnode.clj
Created January 25, 2020 06:18
defnode macro
(defmacro defnode
"This macro defines a new node type. To do this, it does several things:
(1) Defines specs for a new node type. Specifically, it creates a new spec for:
- each additional attribute
- the entire attribute map with the built-in ::id, ::type, and ::labels keys
- the id-attribute vector pair used to add a node to ubergraph
(2) Defines a function make-name-symbol-pair (e.g. make-text-source-pair) that
takes an id argument and any additional arguments and returns an id-attr pair for
We can make this file beautiful and searchable if this error is corrected: Illegal quoting in line 7.
#FORMAT=WebAnno TSV 3.2
#T_SP=webanno.custom.Referent|entity|infstat
#T_RL=webanno.custom.Coref|type|BT_webanno.custom.Referent
#Text=Emperor Norton
1-1 0-7 Emperor person[1] new[1] coref 2-1[2_1]
1-2 8-14 Norton person[1] new[1] _ _
#Text=Joshua Abraham Norton ( c. 1818 – January 8 , 1880 ) , known as Emperor Norton , was a citizen of San Francisco , California , who in 1859 proclaimed himself " Norton I , Emperor of the United States " .
<text id="AUTOGUM_wikinews_debug" revid="DEBUG" sourceURL="DEBUG" type="wikinews">
<p>
<hi rend="bold">Tuesday, June 20, 2006</hi>
</p>
<figure>
<caption>
Rickard Falkvinge, leader of the Swedish "Pirate Party"
</caption>
</figure>
<p>
// This is an adaptation of a logic puzzle presented in SICP
// (https://web.mit.edu/alexmv/6.037/sicp.pdf#page=596) and respectively
// sourced from Dinesman 1968:
//
// > Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an
// > apartment house that contains only five floors. Baker does not live on the
// > top floor. Cooper does not live on the bottom floor. Fletcher does not live
// > on either the top or the bottom floor. Miller lives on a higher floor than
// > does Cooper. Smith does not live on a floor adjacent to Fletcher’s. Fletcher
// > does not live on a floor adjacent to Cooper’s. Where does everyone live?
.annis-wrapped-htmlvis-shenoute_abraham .htmlvis {
font-family: Antinoou, sans-serif;
counter-reset: line 0;
}
.annis-wrapped-htmlvis-shenoute_abraham div.copt_line[line="1"] {
text-indent: 25px;
}
.annis-wrapped-htmlvis-shenoute_abraham div.copt_line[line="2"] {
@lgessler
lgessler / sklearn_demo.py
Created August 6, 2019 18:39
Brief demo of scikit-learn's major API's
from sklearn.linear_model import LogisticRegression, LinearRegression, Lasso
from sklearn.datasets import load_boston
from sklearn.metrics import mean_squared_error, accuracy_score
def main():
X, y = load_boston(return_X_y=True)
print(list(X[1, :]))
[