Skip to content

Instantly share code, notes, and snippets.

;;
;; Not having used clojure with more than one person, I'm curious if code like this
;; would generally be considered too dense/unclear
;;
(defn changes
"Takes two maps and returns a map of the differences"
[suspense-data master-data]
(let [sd (set suspense-data), md (set master-data)
keyz (distinct (map first (clojure.set/difference
;;
;; Several methods that were helpful in development when working with large
;; legacy java objects. I wouldn't be too surprised if somebody told me that
;; emacs does some of this stuff already
;;
;; This function (which is arguably not really an appropriate use of multimethods)
;; is an alternative to the (bean) method, which I found returns a "map" that
;; will throw an exception if
(defn my-split-with
[pred coll]
(let [evaled (map (fn [x] [x (pred x)]) coll)
t (take-while #(% 1) evaled)
d (drop-while #(% 1) evaled)]
[(map first t) (map first d)]))
;; Both of these are inputs at runtime, separately
(def lib "(ns my-ns) (defn x [n] (+ n 5))")
(def code '(x 12))
(eval code) ;; doesn't work of course
;; I want to be able to do this without "using" the namespace
; in repl
(ns x)
(defn y [n] (+ 3 n))
(ns user)
(remove-ns x) ;java.lang.Exception: Unable to resolve symbol: x in this context
(defn- my-split-at
"A reimplementation of clojure.core/split-at that doesn't
compute the sequence twice."
[n coll]
(loop [nn n l [] r coll]
(if (or (zero? nn) (empty? r))
[l r]
(let [[rx & rs] r]
(recur
(dec nn)
(defn- add-seq-at
[colls new-seq]
(let [target (first new-seq)
existing (or (colls target) [])]
(assoc colls target (cons new-seq existing))))
(defn- check-stuff
[seq-seq-map z]
(let [ssmz (seq-seq-map z)]
(if ssmz
@gfredericks
gfredericks / good_arrow.clj
Created January 12, 2011 14:23
Replacing my bad logic with arrow syntax
; Had this
(let [g (nf.graph6/graph6-to-set (:graph6 rec)),
unfairness (nf.graphs/unfairness g)]
...)
; and replaced with this
(let [unfairness (-> rec
:graph6
@gfredericks
gfredericks / return_true.clj
Created March 5, 2011 16:40
detect_cycle.clj
(defn return-true
[f ob]
(loop [[a & as] (iterate f ob), [_ b & bs] (iterate f ob)]
(if (= a b)
true
(recur as bs))))
@gfredericks
gfredericks / stacktrace.txt
Created March 26, 2011 13:21
output of "lein test"
Reflection warning, plaza/triple_spaces/multi_remote_server/auxiliary.clj:42 - call to write can't be resolved.
Reflection warning, plaza/triple_spaces/multi_remote_server/auxiliary.clj:69 - call to write can't be resolved.
Reflection warning, plaza/triple_spaces/multi_remote_server/auxiliary.clj:75 - reference to field getBytes can't be resolved.
Reflection warning, plaza/triple_spaces/multi_remote_server/auxiliary.clj:99 - call to write can't be resolved.
Reflection warning, plaza/triple_spaces/multi_remote_server/auxiliary.clj:120 - call to write can't be resolved.
Reflection warning, plaza/triple_spaces/multi_remote_server/auxiliary.clj:140 - call to write can't be resolved.
Reflection warning, plaza/triple_spaces/multi_remote_server/auxiliary.clj:154 - reference to field getBytes can't be resolved.
Reflection warning, plaza/triple_spaces/multi_remote_server/auxiliary.clj:176 - call to write can't be resolved.
Reflection warning, plaza/triple_spaces/multi_remote_server/auxiliary.clj:188 - call to write ca