Skip to content

Instantly share code, notes, and snippets.

@CampingScorpion
CampingScorpion / gist:1266137
Created October 6, 2011 00:12
StringBuilder Monoid
object Monoid {
implicit val StringBuilderMonoid = new Monoid[StringBuilder] {
override def zero = new StringBuilder("")
override def append(sb1: StringBuilder, sb2: StringBuilder) = {
val delimiter = if (sb1.toString == "" ||
sb2.toString == "") ""
else ", "
sb1.append(delimiter).append(sb2)
}
}
@CampingScorpion
CampingScorpion / goodbye-cruel-world.clj
Created October 14, 2011 02:49
Goodby Cruel World, in Clojure w/ Seesaw
(ns cruelworld.core
(:gen-class )
(:use seesaw.core))
(defn -main [& args]
(invoke-later
(-> (frame :title ":(",
:content "Goodbye cruel world",
:on-close :exit)
pack!
@CampingScorpion
CampingScorpion / do-at*.clj
Created October 24, 2011 01:04
evaluate some code at a given DateTime
(defn do-at* [date-time f]
(DateTimeUtils/setCurrentMillisFixed (.getMillis date-time))
(try
(f)
(finally (DateTimeUtils/setCurrentMillisSystem))))
;; you might use this code like this:
(do-at* (DateMidnight. 2000 1 1)
(fn [] (DateMidnight.))) ;;=> (DateMidnight. 2000 1 1)
(defmacro do-at [date-time & body]
"like clojure.core.do except evalautes the
expression at the given time"
`(do-at* ~date-time
(fn [] ~@body)))
;; nice. Now we can see the actual code that was hiding
;; behind all of those parens and fn creation:
(do-at (DateMidnight. 2000 1 1)
@CampingScorpion
CampingScorpion / gist:1335513
Created November 3, 2011 01:29
a variation of interleave that handles 0-infinite sequences
;; I dub thee, interleave++
(defn interleave++
"like interleave from core, but does something sensible with 0 or 1 collection"
([]
(lazy-seq []))
([coll]
(lazy-seq coll))
([coll1 coll2 & colls]
(apply (partial interleave coll1 coll2) colls)))
@CampingScorpion
CampingScorpion / gist:1346666
Created November 8, 2011 00:40
first-truthy-fn
(defn first-truthy-fn
"Returns the first function in a seq of functions
that evaluates to truthy for the given arguments"
[all-preds & args]
(loop [[p & more-ps] all-preds]
(when p
(if (apply p args) p (recur more-ps)))))
;; filter version that fails to be lazy:
;; I want to replace this:
(defn- unfinished* [names]
(let [declarations (for [name names]
`(do
(defn ~name [& args#]
(throw (user-error (str "#'" '~name " has no implementation. It's used as a prerequisite in Midje tests."))))
;; A reliable way of determining if an `unfinished` function
;; has since been defined.
@CampingScorpion
CampingScorpion / gist:1499725
Created December 20, 2011 01:09
macroexpand-scan / macroexpand-reductions
(defn macroexpand-scan
([form]
(macroexpand-scan form []))
([form results]
(let [expanded (macroexpand-1 form)]
(if (= expanded form)
results
(recur expanded (conj results expanded))))))
;; Example
@CampingScorpion
CampingScorpion / gist:1501358
Created December 20, 2011 12:05
steady-state
(defn steady-state
([f x]
(steady-state f x []))
([f x results]
(let [calculated (f x)]
(if (= calculated x)
results
(recur f calculated (conj results calculated))))))
(def macroexpand-scan (partial steady-state macroexpand-1))
@CampingScorpion
CampingScorpion / gist:1518307
Created December 24, 2011 20:44
nested fact descriptions
(facts "about mathematics"
(fact "addition is commutative"
(+ 1 2) => 3
(+ 2 1) => 4) ;; FAIL
(fact "multiplication is commutative"
(* 2 5) => 10
(* 5 2) => 11)) ;; FAIL
;; These would report as: