Skip to content

Instantly share code, notes, and snippets.

View rauhs's full-sized avatar

Andre W. rauhs

  • Bavaria, Germany
View GitHub Profile
@rauhs
rauhs / dynamic-xf-async.clj
Created August 15, 2015 15:53
Changing your transducer during the runtime of your core.async channel (could use some optimizaiton caching the instantiation)
(def xf-a
(atom (map inc)))
(defn xf [& args-out]
(fn [& args]
;; TODO: Cache the inner (apply...)
(apply (apply @xf-a args-out) args)))
(def ch (chan 1 xf))
@rauhs
rauhs / ls-files.clj
Last active August 29, 2015 14:27
Clojure: List files in directory
(ns x.y
(:import
[java.io File]))
(defn ls-match
"Given the directory handle d, lists all files matching the given
regex. Returns the Java File instances.
Example:
(ls-match (File. \"./dir\") #\"(?i).drl$\")"
([^File d]
@rauhs
rauhs / assoc-if-nil.clj
Created August 22, 2015 22:04
Macro: Assoc a map only if the key is nil. Does NOT evaluate the value unless it is nil.
(defmacro assoc-if-nil
"Takes a map as the first argument and a succession of key value pairs that
are used to set the key to value if the key of the map is nil. The value part
is only evaluated if the key is nil (thus different semantics to (merge)).
Example:
(assoc-if-nil {:a {:b :set}}
[:a :b] :non-def
[:a :c] :non-def
:d :non-def)
;; =>{:a {:b :set, :c :non-def}, :d :non-def}"
@rauhs
rauhs / quantizer.clj
Created August 22, 2015 22:06
Returns a function that quantizes data
(defn quantizer
"Returns a function that quantizes input data which when called with 'x' returns:
o <1st val> if -Inf < x <= <1st bound>
o <2st val> if <1st bound> < x <= <2st bound>
o ...
o <last val> if <last-1 bound> < x <= <last bound>
o >max if x > <last bound>
where m is a vector of vectors where the first element specifies the boundary and
the second element the value which to return.
@rauhs
rauhs / between-schema.clj
Last active September 18, 2015 15:55
Clojure prismatic schema for max min values. Kewords: Between, less-than greater-than
(defn between
[min max]
(s/pred #(<= min % max) (list 'between min max)))
(ns srs-c.utils.keyboard
(:require-macros
[klang.macros :refer [log! debg! trac! info! warn! erro! crit! fata! env!]]
[srs-c.macros :refer [onlydev onlyprod]])
(:require [goog.events])
(:import [goog.ui KeyboardShortcutHandler]))
(defn install-shortcut!
"Installs a Keyboard Shortcut handler.
@rauhs
rauhs / datomic-squuid.lua
Last active June 24, 2016 10:09
Generate a squuid in nginx (openresty)
-- Datomic SQUUID layout:
-- xxxxxxxx-xxxx-4xxx-Vxxx-xxxxxxxxxxxx
-- ^^^^^^^^ ^^^^ ^
-- | | |^^^ ^
-- | | | | |^^^-^^^^^^^^^^^^
-- | | | | | |
-- | | | | | |- random
-- | | | | |- IETF variant {8,9,a,b}
-- | | | |- random
-- | | |- Version 4
(spec/def :datascript.db/id int?)
(spec/def :datomic.db/id #(instance? DbId %))
(spec/def :db/id (spec/or :datascript :datascript.db/id
:datomic :datomic.db/id))
(defn datomic-id?
"Checks a conformed spec and ensures the :db/id was a datomic id"
[{[kw] :db/id}]
(= kw :datomic))
(defn datascript-id?
"Checks a conformed spec and ensures the :db/id was a datascript id"
(defn take-while+
"Identical to take-while but includes the false element."
[pred coll]
(lazy-seq
(when-let [[f & r] (seq coll)]
(if (pred f)
(cons f (take-while+ pred r))
[f]))))
(defn partition-after
@rauhs
rauhs / map-all.clj
Last active November 24, 2016 17:51
(defn map-all
"Like map but if given multiple collections will call the function f
with as many arguments as there are elements still left."
([f] (map f))
([f coll] (map f coll))
([f c1 & colls]
(let [step (fn step [cs]
(lazy-seq
(let [ss (keep seq cs)]
(when (seq ss)