Skip to content

Instantly share code, notes, and snippets.

View favila's full-sized avatar

Francis Avila favila

View GitHub Profile
@favila
favila / wrap-highlighted.cljc
Created August 22, 2015 00:31
Wrap-highlighted, for splitting strings with elasticsearch highlighting.
(defn wrap-highlighted
"Return a vector of string parts where highlighted runs are replaced with (f highlighted-string)."
[f s]
(->> (re-seq #"<em>(.+?)</em>|.+?(?=<em>|$)" s)
(map (fn [[all highlighted]]
(if (nil? highlighted)
all
(f highlighted))))))
@favila
favila / cond-assoc-from.clj
Created July 13, 2015 17:14
Conditionally assoc values of a source map to a target map.
(defmacro cond-assoc-from
"Using a source ILookup (map, vector, etc) and a target IAssoc (map), for each
clause bind the value of source at given source-key to a binding expression
and assoc the target-expression (evaluated with bindings) to the target under target-key.
If the source-key's value is nil, skip the clause. Example:
(cond-assoc-from {:a [1 2]} {}
[a-1 a-2] :a :A (inc a-2)
b :b :B (inc b))
;=> {:A 3}"
@favila
favila / tempid-idx.clj
Created June 1, 2015 23:26
AtomicInteger counter with overflow handled correctly
(import '[java.util.concurrent.atomic AtomicInteger])
(set! *unchecked-math* :warn-on-boxed)
(def ^AtomicInteger counter (AtomicInteger.))
(defn next-tempid-idx [^AtomicInteger i]
(unchecked-subtract -1000000 (bit-and (.getAndIncrement i) 0x7fffffff)))
@favila
favila / bootstrap-txes-0-9-5173.edn
Created June 1, 2015 20:51
Find the bootstrap transactions for a datomic database. Bootstrap transactions are the first few transactions which create all the datoms necessary for datomic to work (e.g., the initial partitions, types, attributes, and tx functions). They are not accessible directly via the transaction log.
;; Result of bootstrap-txes function above under datomic 0.9.5173
;; Bootstrap transactions are 0,
;; More bootstrap transactions or datoms may be added in later datomic versions.
;; First non-bootstrap transaction is always T >= 1000.
;; Format is [[t [[e a v tx added] ...] ...] ...]
[[0
[[0 10 :db.part/db 13194139533312 true]
[1 10 :db/add 13194139533312 true]
[2 10 :db/retract 13194139533312 true]
@favila
favila / jforms.html
Created March 6, 2015 04:23
Old experiment with the XForms master dependency directed graph algorithm http://www.w3.org/TR/xforms11/#rpm-processing-recalc-mddg
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>xforms dependency algorithm</title>
</head>
<body>
<div>
<form action="" method="post" accept-charset="utf-8">
<ol style="list-style-type:lower-latin">
@favila
favila / restore_datoms.clj
Last active September 30, 2021 03:50
Restore a datomic database "manually", i.e. from raw datoms. Useful for memory databases. Context: https://groups.google.com/d/msg/datomic/BkTdKYB3WpE/AKfqKYqPONMJ
(ns favila.datomic-util.restore-datoms
"A \"manual\" datomic database restore.
Writes raw datoms (stored in a stream written by d/datoms) to an empty database.
Useful for memory databases: you can write out all the datoms in it, then read
them into another database. (Note mem dbs have no log or retractions)."
(:require [datomic.api :as d]
[clojure.edn :as edn]))
(defrecord datom [e a v tx added?])
@favila
favila / gen-transactor-props.sh
Created February 2, 2015 21:15
Generate a datomic transactor property file for google cloud mysql storage engine with SSL and client authentication
#!/bin/sh
# Generate datomic transactor properties file and command line arguments.
# Specific scenario: using Google Mysql Cloud storage with SSL and client
# authentication.
OUTFILE='transactor.properties'
#TRANSACTOR_ARGS_FILE='transactor-arguments.txt'
@favila
favila / nc-http.sh
Created January 28, 2015 00:32
Netcat-based logging http server for debugging. Writes raw http requests to individual files and replies with a 204.
#!/bin/sh
# A netcat-based logging http server for debugging.
#
# Writes raw http requests to individual files and replies with a 204.
PORT=${1-"8085"}
OUTFILEPREFIX=${2-"request"}
REPLY='HTTP/1.1 204 NO CONTENT\r\nconnection: close\r\n\r\n'
OUTFILE=''
cleanup () {
@favila
favila / datomic-mysql-bootstrap.sql
Created January 22, 2015 17:49
Better MySQL bootstrap setup for datomic's datomic_kvs table
-- Optimized MYSQL schema for datomic
-- Unfortunately the bin/sql/mysql-*.sql bootstrapping files for datomic are not
-- very good, and can actually cause failures if not adjusted.
-- One symptom of this is the following error:
-- SQL Error (1071): Specified key was too long; max key length is 767 bytes.
-- Reported here: https://support.cognitect.com/entries/28462168-MySQL-Caveats
-- This is caused by the default collation for the `id` column possibly being
@favila
favila / visualuuid.cljs
Last active August 29, 2015 14:13
Experiments with representing a uuid visually. A bit of a failed experiment: the patterns generated do not have enough distinct visual features for humans: looks like colored noise.
;; These colors are colorblind-safe
(def visual-uuid-colors #js ["#f0e442" "#0072b2" "#d55e00" "#cc79a7"])
(def visual-uuid-shapes "▄▌▀▐" #_"◢◣◤◥" #_" █▖▗▘▝▚▞")
(defcomponent VisualUUID
"Draw an iconographic representation of a uuid meant for easy visual
recognition and comparision.
Every 4 bits becomes a color+shape; full uuid is an 8x4 grid of shapes."
[uuid]