Skip to content

Instantly share code, notes, and snippets.

View favila's full-sized avatar

Francis Avila favila

View GitHub Profile
@favila
favila / decodeUtf8.js
Last active August 29, 2015 14:00
OBSOLETE: see https://github.com/favila/utfate which is improved. Fast utf8 decoder in JS; faster than the TextDecoder polyfill at https://github.com/inexorabletash/text-encoding and about 66% slower than the native TextDecoder on Firefox. Benchmarks: http://jsperf.com/utf8-decoding
SFCCA = Function.prototype.apply.bind(String.fromCharCode, null);
SFCC = String.fromCharCode
/**
* Dispatch table for multi-byte UTF8 code points.
*
* Index of (utf8_prefix_byte - 0x80) is length of UTF8 byte
* sequence + 1. 0 means invalid prefix byte.
*
* @type {!Int8Array}
* @const
@favila
favila / rollback.clj
Last active August 29, 2015 14:05
Undo datomic transaction
(defn rollback
"Reassert retracted datoms and retract asserted datoms in a transaction,
effectively \"undoing\" the transaction.
WARNING: *very* naive function!"
[conn tx]
(let [tx-log (-> conn d/log (d/tx-range tx nil) first)
txid (-> tx-log :t d/t->tx)
newdata (->> (:data tx-log)
(remove #(= (:e %) txid))
@favila
favila / async-util.clj
Created November 28, 2014 17:06
Some missing pieces of core.async. as-transducer: Make a transducer function easily without Clojure 1.7. go-pipe: async/pipe, but returns the go-loop. fast-pipeline-blocking: faster than async/pipeline-blocking, but unordered results. blocking-consumer: consume a channel with multiple worker threads.
(ns favila.async-util
"Some missing pieces of core.async.
as-transducer: Make a transducer function easily without Clojure 1.7.
go-pipe: async/pipe, but returns the go-loop.
fast-pipeline-blocking: faster than async/pipeline-blocking, but unordered results.
blocking-consumer: consume a channel with multiple worker threads."
(:require [clojure.core.async :as async
:refer [go go-loop <! >! <!! >!! close! take! put! chan]]))
@favila
favila / arguments-to-array.cljs
Created December 16, 2014 17:56
Function (not macro) to clone javascript arguments object to array in the most jit-friendly way possible.
(defn- arguments-to-array
"This is the most jit-friendly way to turn arguments into an array.
js-this is a uint32 start index.
Call *exactly* like so: (.apply arguments-to-array start-index (js-arguments))
Any other call pattern will prevent optimizations in calling function.
Returns a real js array of its arguments starting from start-index.
@param {...*} var_args"
[var_args]
@favila
favila / xyz.clj
Created December 26, 2014 19:40
Using iota to process an XYZ file in parallel over its chunks. Easily generalized to any situation where you want to fold over groups where the source is not grouped. Prompted by this post: https://groups.google.com/forum/#!topic/clojure/OkxAshQ0JTU
(ns xyz
"Utilities for determining chunk position in XYZ files and processing
chunks using reducers.
Uses iota, which uses mmap()."
(:require [clojure.core.reducers :as r]
iota))
(defn- step-chunk-starts
([] [(vector-of :int) 0 :find-start])
@favila
favila / as-transducer.clj
Created December 29, 2014 17:58
as-transducer: convenience function to make a full transducer from a reducing-step function which accepts three arguments: the transforming function (xf), the reduced value, and the step value from the reduction.
(defn as-transducer [f]
"Convert function f into a transducer function.
This is merely a more convenient way to write an efficient reducing-step
function without the transducer signature.
The function should have the signature `(f transformer opaque-collection value)`
and return either `opaque-collection` or the return value of
`(transformer opaque-collection value)` to return new `opaque-collection`
things with `value` \"added\" to it. It can also box its return value with
@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]
@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 / 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 / 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'