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
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) | |
} | |
} |
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 cruelworld.core | |
(:gen-class ) | |
(:use seesaw.core)) | |
(defn -main [& args] | |
(invoke-later | |
(-> (frame :title ":(", | |
:content "Goodbye cruel world", | |
:on-close :exit) | |
pack! |
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 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) |
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
(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) |
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
;; 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))) |
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 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: |
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
;; 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. |
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 macroexpand-scan | |
([form] | |
(macroexpand-scan form [])) | |
([form results] | |
(let [expanded (macroexpand-1 form)] | |
(if (= expanded form) | |
results | |
(recur expanded (conj results expanded)))))) | |
;; Example |
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 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)) |
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
(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: |