Skip to content

Instantly share code, notes, and snippets.

View favila's full-sized avatar

Francis Avila favila

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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)
@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 / 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]
(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")