Skip to content

Instantly share code, notes, and snippets.

View favila's full-sized avatar

Francis Avila favila

View GitHub Profile
@favila
favila / unsigned-to-signed-literal.clj
Created March 10, 2016 23:14
Basic code pattern to get signed hex literal (for java) from an unsigned hex literal
(defn signed-literal [^String unsigned-hex-str]
(let [s (-> (Long/parseUnsignedLong unsigned-hex-str 16)
(Long/toString 16))]
(if (.startsWith s "-")
(str "-0x" (subs s 1))
(str "0x" s))))
@favila
favila / stm-example-core.clj
Created April 2, 2016 09:22
Somewhat realistic example of using refs and dosync, compared with an equivalent approach using an atom.
(ns stm-example.core
"A somewhat realistic use of Clojure refs and dosync.
In most cases you should use an atom: it will be simpler and faster.
But you may need to use refs when:
* You need to return something other than the entire state from a swap!. E.g.,
the id of a newly-created user, or whether an action succeeded. You can
emulate this by adding a \"return value\" key to the atom on any state
@favila
favila / advent_a6.clj
Last active April 3, 2016 06:31
Solutions to advent of code, day 6 in clojure. Written in response to this post: https://groups.google.com/d/msg/clojure/iQRKhOKxh5g/5P5_QticBAAJ
(ns advent.a6
(:require [clojure.java.io :as io])
(:import (clojure.lang PersistentVector)))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(defn input []
(slurp (io/resource "day6-part1-input")))
@favila
favila / datomic-edn-handlers.clj
Created April 5, 2016 00:24
Utilities for making it easier to read edn files. Also includes tag readers that datomic uses.
(ns favila.read-edn.tag-readers
"Common tag-reader maps for edn."
(:require datomic.db
datomic.function
datomic.codec)
(:import java.net.URI))
(defmethod print-method URI [^URI x ^Writer w]
(doto w
(.write "#uri ")
@favila
favila / datomic-reset-attributes.clj
Last active January 12, 2025 14:04
Datomic transaction functions to "reset" attributes: i.e. make them have a post-transaction value you specify without having to enumerate the retractions.
(def tx-fns
[{:db/ident :db.fn/reset-attribute-values
:db/doc "Transaction function which accepts an entity identifier, attribute identifier
and set of values and expands to any additions and retractions necessary to
make the final post-transaction value of the attribute match the provided
values. Attribute values must be scalars.
If multiple values are provided on a cardinality-one attribute you will get a
datom conflict exception at transaction time."
:db/fn (d/function
@favila
favila / sha1-hash-stream.clj
Created July 13, 2016 21:04
sha1 hash a stream in clojure
(defn sha1-hash-stream [^InputStream stream]
(let [buffer (byte-array 8192)
md (MessageDigest/getInstance "SHA-1")]
(loop []
(let [taken (.read stream buffer 0 8192)]
(if (> taken 0)
(do (.update md buffer 0 taken)
(recur))
(.digest md))))))
@favila
favila / core-async-promise-all.cljs
Created July 28, 2016 22:38
Given a collection of takeable items, return a promise channel which will contain a vector of the next available value taken from every item, preserving order if it exists in the original collection. If any takeable is closed the promise will be closed. Runs as synchronously as possible using poll! and offer!, only becoming async if one of the t…
(defn promise-all
"Given a collection of takeable items, return a promise channel which will
contain a vector of the next available value taken from every item, preserving
order if it exists in the original collection. If any takeable is closed
the promise will be closed.
Runs as synchronously as possible using poll! and offer!, only becoming async
if one of the takeable items is not immediately takeable.
Example:
package mytransit;
import com.cognitect.transit.TransitFactory;
import com.cognitect.transit.WriteHandler;
import com.cognitect.transit.Writer;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

Keybase proof

I hereby claim:

  • I am favila on github.
  • I am favila (https://keybase.io/favila) on keybase.
  • I have a public key whose fingerprint is 594B BA31 C16A BA70 3AF4 AFB5 173E 02EE 2F85 597C

To claim this, I am signing this object:

;; same thing using reduction over d/datoms.
;; Objective is to avoid realizing too much in memory
;; May still be slow, but won't OOM
(defn orgs->max-fks [db]
(reduce (fn [o->fk [content-eid _ content-fk]]
(let [org-id (-> (d/entity db content-eid)
:content/collection
:collection/organization
:db/id)]