Skip to content

Instantly share code, notes, and snippets.

;; 1. Current version -- note the use of `when-valid`... it is essentially duplicating the work the
;; syntax-validation-m monad should be able to handle, so I've been attempting to
;; clean it up / refactor it to use only the monad
(defmacro expect
"Run the call form, check that all the mocks defined in the fakes
(probably with 'fake') have been satisfied, and check that the actual
results are as expected. If the expected results are a function, it
will be called with the actual result as its single argument.
@CampingScorpion
CampingScorpion / gist:2393807
Created April 15, 2012 16:51
See all earmuff vars in clojure.core
(doseq [[sym the-var] (ns-publics 'clojure.core)
:when (.startsWith (name sym) "*")]
(println (str (name sym) ":\n" (:doc (meta the-var)) "\n")))
@CampingScorpion
CampingScorpion / gist:2915666
Created June 12, 2012 06:47
basic nested sort
(defn nested-sort [x]
(cond (sequential? x)
(if (instance? java.lang.Comparable (first x))
(sort (map nested-sort x))
(map nested-sort x))
(map? x)
(if (and (not= {} x)
(instance? java.lang.Comparable (key (first x))))
(into (sorted-map) (map-values nested-sort x))
@CampingScorpion
CampingScorpion / gist:3898489
Created October 16, 2012 10:13
Clojure joda DateTime instant reader
(ns groupon.joda-instant-reader
(:require [clojure.instant :as i])
(:import org.joda.time.DateTime))
(defmethod print-method org.joda.time.DateTime
[^org.joda.time.DateTime d ^java.io.Writer w]
(#'i/print-date (java.util.Date. (.getMillis d)) w))
(defmethod print-dup org.joda.time.DateTime
user> (defn foo [_ _ s]
`(println ~s))
#'user/foo
user> (foo nil nil 5)
(clojure.core/println 5)
user> (alter-meta! #'foo assoc :macro true)
{:arglists ([_ _ s]), :ns #<Namespace user>, :name foo, :macro true, :line 1, :file "NO_SOURCE_FILE"}
user> (foo 5)
5
nil
@CampingScorpion
CampingScorpion / gist:3996452
Created November 1, 2012 20:50
checked-route w/ custom reporter
(deftype JsonErrorReporter
;; Hook for clj-schema.validation/validation-errors, to format the errors
;; for consumption on the JS side
[]
val/ErrorReporter
(non-map-error [_ {:keys [parent-path map-under-validation]}]
{:type :non-map})
(extraneous-path-error [_ {:keys [map-under-validation]} extra-path]
{:type :extraneous-path
@CampingScorpion
CampingScorpion / gist:3996454
Created November 1, 2012 20:50
checked-route w/ custom reporter
(deftype JsonErrorReporter
;; Hook for clj-schema.validation/validation-errors, to format the errors
;; for consumption on the JS side
[]
val/ErrorReporter
(non-map-error [_ {:keys [parent-path map-under-validation]}]
{:type :non-map})
(extraneous-path-error [_ {:keys [map-under-validation]} extra-path]
{:type :extraneous-path
@CampingScorpion
CampingScorpion / gist:4014270
Created November 4, 2012 23:09
type hint vararg
(defn- get-holidays [^HolidayManager holiday-manager ^Interval interval]
(.getHolidays holiday-manager interval (into-array String [])))
@CampingScorpion
CampingScorpion / gist:4134522
Created November 23, 2012 08:27
doseq-indexed
;; Example:
;; (doseq-indexed idx [name names]
;; (println (str idx ". " name)
(defmacro doseq-indexed [index-sym [item-sym coll] & body]
`(let [idx-atom# (atom 0)]
(doseq [~item-sym ~coll]
(let [~index-sym (deref idx-atom#)]
@CampingScorpion
CampingScorpion / gist:7079749
Last active December 26, 2015 02:38
A first foray into generating clojure.core.typed annotations from a syntax looking like Prismatics schema library. Still it needs to map the schemas to corresponding type annotations, and more complexity I'm sure I haven't even thought of yet.
(ns millega.foo
(:require [clojure.core.typed :as t]))
(t/ann f1 [t/AnyInteger -> t/AnyInteger])
(defn f1 [n]
(* 2 n))
(defmacro defn2 [name _colon_dash result-type-ann args+anns & body]
(let [args (vec (take-nth 3 args+anns))