This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 ") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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"))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[datomic.api :as d]) | |
(defn entity-changes | |
"Return transaction history for an entity and its components. | |
Returns a sequence of maps, each is the transaction map (tx metadata) plus a | |
key `:entity-changes`, which is a nested map of keys: entity id, attribute | |
ident, `:db/add` or `:db/retract`, and the value added or retracted, e.g.: | |
{12345678910 {:long-card-1-attr {:db/retract 1 | |
:db/add 0} | |
:long-card-many-attr {:db/add #{0}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn grouper-by | |
"Return a reducing function which reduces items in groups (determined by | |
result of `(keyfn item)`) independently using optional transducer | |
`group-xform` and reduction function `group-rf` (which should have 3 arities). | |
The supplied reducing and transducing functions may be stateful: their state | |
will be isolated to a particular group. | |
The returned reducing function should be run with `transduce` | |
or `finalizing-reduce` (not `reduce`, which does not use the \"finalize\" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn unique? | |
"Return true if a collection's items are unique, else false | |
With a key function, will return true if `(map keyfn coll)` is unique, else false." | |
([coll] (unique? coll identity)) | |
([coll keyfn] | |
(or (set? coll) (map? coll) | |
(some? (reduce | |
(fn [seen x] | |
(let [k (keyfn x)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn buffered-reduce | |
"Return a transducer wrapping `reduction-fn` which holds on to an internal | |
buffer volatile (initialized with nil) which will be flushed (via `reduce`) | |
when the transducer is closed. | |
The `reduction-fn` should accept a combining function, an accumulating value, | |
a current value, and a buffer volatile. This function should return a new | |
accumulating value via 0, 1 or many calls to `(xf r new-value)`. It may also | |
manipulate the contents of the volatile for its own use. If anything is found | |
in the buffer when the reduction is complete, its items will be added to the |