Skip to content

Instantly share code, notes, and snippets.

View rauhs's full-sized avatar

Andre W. rauhs

  • Bavaria, Germany
View GitHub Profile

This document is my personal writeup of trying to make sense of all the clojure(script) tooling chain. It might contain errors and it actually contains a few questions

Tooling:

Nomenclature/Terminology

REPL: A prompt in its simples form.

  1. Waits for and Reads input,
@rauhs
rauhs / parse-duration.clj
Last active December 15, 2017 14:52
Clojure named caputred Regex with application for duration parsing "4 weeks 8d 20h 2mins"
(defn re-named-groups
"Returns the groups from the most recent match/find. If there are no
nested groups, returns a string of the entire match. If there are
nested groups, returns a vector of the groups, the first element
being the entire match."
[^java.util.regex.Matcher m kw]
(let [gc (. m (groupCount))]
(if (zero? gc)
(. m (group))
(into {} (map (fn[x] [x (. m (group (name x)))]) kw)))))
@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 / humanize-schema-errors.clj
Last active May 27, 2023 05:29
Translate prismatic's schema.core errors to a human readable form. Use this for presenting validation errors to users. Don't use this for programming errors like a missing map key etc.
(ns x.y
(:use [plumbing.core]) ;; Just for the map-vals
(:require [clojure.walk :refer [postwalk prewalk prewalk-demo postwalk-demo]]
[clojure.core.match :refer [match]]
[schema.utils :refer [named-error-explain validation-error-explain]]
[schema.core :as s])
(:import (schema.utils NamedError ValidationError)))
;; Partially FROM:
;; https://github.com/puppetlabs/clj-schema-tools
@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)))
@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 / Makefile
Last active July 7, 2020 19:44
Compiling clojurescript + figwheel without boot nor leiningen. Using leiningen to manage dependencies. Print file size stats (raw, gzip, brotli) for production builds
CLJ_NREPL_PORT:=22340
FIGWHEEL_PORT:=22345
CLJS_JAR_VERSION:=1.7.48
CLJS_JAR_URL:=https://github.com/clojure/clojurescript/releases/download/r$(CLJS_JAR_VERSION)/cljs.jar
.PHONY: def_target
def_target : null
@rauhs
rauhs / om.next+pedestal+transit.clj
Last active December 10, 2017 17:08
Om.next tempid handling for pedestal. Interceptors.
(ns srs-s.routes.core
(:require [io.pedestal.http :as pedestal]
[io.pedestal.http.route.definition :refer [defroutes]]
[io.pedestal.interceptor.helpers :as interceptor]
[io.pedestal.http.body-params :as body-params]
[ring.util.response :as ring-response]
[cognitect.transit :as transit]
[om.next.server :as om]
[om.tempid :as tempid])
(:import [java.io OutputStream]