Skip to content

Instantly share code, notes, and snippets.

View favila's full-sized avatar

Francis Avila favila

View GitHub Profile
@favila
favila / git-archive-branch.sh
Created October 2, 2013 20:22
git-archive-branch.sh Replace a git branch with an archived tag.
#!/bin/sh
# git-archive-branch.sh
#
BRANCHNAME=
PUSH=
usage() {
cat 1>&2 << EOF
usage: $(filename $0) [-p] [-h] BRANCHNAME
@favila
favila / defenum.clj
Last active December 29, 2015 01:29
Macro to define a google closure compiler enum from clojurescript.
(ns defenum
(:require [clojure.string :as str]
cljs.compiler))
(defn unzip
([s] (unzip 2 s))
([n s] (apply map vector (partition n s))))
(defmacro jso
"Like js-obj, except the keys are emitted as unquoted munged symbols. Keys
@favila
favila / unique.py
Created January 28, 2014 05:29
Various ways to test if all members of a collection are unique in Python 2.7, with timing harness.
import re
import array
import collections
import bisect
re_dup_g = re.compile(r'(.).*\1', re.DOTALL)
re_dup_ng = re.compile(r'(.).*?\1', re.DOTALL)
def isunique_reg(s, search=re_dup_g.search):
@favila
favila / breeze.util.uuid.clj
Created February 17, 2014 03:27
Generate version 5 (sha1-based) uuids in clojure.
(ns breeze.util.uuid
"Utilities for generating version 5 (SHA-1 hashing) uuids.
This implementation should conform to RFC 4122, section 4.3.
Includes shortcuts for generating uuids for breeze rule hashes."
(import java.security.MessageDigest
java.util.UUID
java.nio.ByteBuffer
java.nio.charset.StandardCharsets))
(def rule-namespace-uuid
@favila
favila / murmur3_32.js
Last active August 29, 2015 13:56
Port just enough of Murmur3 32bit to javascript for clojurescript. Goal is to produce identical results and capabilities as this class: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Murmur3.java
/**
* Javascript port of Clojure's flavor of Murmur3.
* See https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Murmur3.java
*
* Find at: https://gist.github.com/favila/9088146
* By favila (Francis Avila) 2014-02-19
*/
goog.provide('cljs.lang.murmur3');
goog.require('cljs.core');
@favila
favila / git-squash.sh
Created February 24, 2014 05:40
git-squash: script to create a squashed patch from a branch.
#! /bin/sh
# Produce a squash-commit patch from a branch of changes
MASTER=$1
PATCHBRANCH=$2
SQUASHBRANCH="$PATCHBRANCH-squash"
MESSAGE=$3
git checkout -b $SQUASHBRANCH $MASTER &&
git merge --squash $PATCHBRANCH &&
git commit -a -m "$MESSAGE" &&
@favila
favila / murmur3.cljs
Created February 24, 2014 22:28
Implementation of murmur3 in clojurescript. Is a port of https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Murmur3.java See also a js version at https://gist.github.com/favila/9088146
(ns murmur3
"Implementation of clojure.lang.Murmur3 in clojurescript (assuming javascript numerics).
by Francis Avila 2014-02-24")
(def imul
"32-bit signed integer multiply with overflow; alias of js/Math.imul.
Does not follow unchecked-multiply-int semantics! Only two args accepted; if
args are missing returns 0."
(if (exists? (aget js/Math "imul"))
(aget js/Math "imul")
@favila
favila / zip-partition.clj
Created March 25, 2014 15:49
Lazy Clojure(Script) function to return row-oriented slices of sequential data suitable for display in a table. E.g. [0 1 2 3 4] -> ((0 3)(1 4)(2 nil))
(defn zip-partition
"Return a sequence of length n of sequences of every nth item, each offset
by one. Suitable for building the rows of a table with n columns where the
sequence items flow down columns then rows. Example:
(zip-partition [0 1 2 3 4] 2) -> ((0 4)
(1 nil)
(2 nil)
(3 nil))"
[s n]
@favila
favila / humanname.clj
Created April 16, 2014 00:05
Create HumanName.text from a HumanName
(defn nametext [hn]
(->> [:fhir-attr/HumanName.prefix :fhir-attr/HumanName.given
:fhir-attr/HumanName.family :fhir-attr/HumanName.suffix]
(mapcat #(get hn %))
(filter not-empty)
(interpose \space)
(apply str)))
(defn humanname-text-tx [d]
(->> (d/datoms d :avet :phi.element/type "fhir-type/HumanName")
@favila
favila / storefront-tempid.clj
Created April 24, 2014 20:20
Hack to fit tempids into JS int range for storefront.
(def ^:const SIGN-BIT (bit-shift-left 1 63))
(defn tempid-cljs->clj [tid]
(if (neg? tid)
(let [cljtid (-> tid (-) (bit-or SIGN-BIT))]
(d/tempid (d/part cljtid) (d/tx->t cljtid)))
tid))
(defn tempid-clj->cljs [tid]
(if (neg? tid)