Skip to content

Instantly share code, notes, and snippets.

treeSeq :: (a -> [a]) -> a -> [a]
treeSeq children root = root : concatMap (treeSeq children) (children root)
data Tree a = Leaf a
| Branch (Tree a) (Tree a)
deriving (Show, Eq)
children :: Tree a -> [Tree a]
children (Leaf a) = []
children (Branch x y) = [x, y]
(def v (into [] (repeatedly 1e5 #(rand 100))))
(defn rnd [x]
(Math/round x))
(defn prnd [x]
(let [^double x x]
(Math/round x)))
(time (r/fold + (r/map rnd v)))
(defn trim-head [coll]
(loop [so-far [], indexes {}, index 0, coll (seq coll)]
(if-not coll
{:origin nil, :pattern so-far} ;; ?? not specified in question
(let [x (first coll), xs (rest coll)]
(if (contains? indexes x)
{:origin (subvec so-far 0 (indexes x))
:pattern (subvec so-far (indexes x))}
(case x
0 {:origin so-far, :pattern [x]}
(let [multiname 'foo
func :check
kvs {:a 1 :b 2 :c 3}]
`(do
(defmulti ~multiname ~func)
~@(for [[k v] kvs]
`(defmethod ~multiname k [m#] v))))
(defmacro using "ns can be either a symbol designating a namespace, or
a list like (ns :only [a b c])."
[ns & body]
(let [[ns vars] (if (sequential? ns)
[(first ns) (nth ns 2)] ;; skip the :only
[ns (keys (ns-publics (find-ns ns)))])]
`(symbol-macrolet [~@(for [var vars,
clause [var `(quote ~(symbol (name ns) (name var)))]]
clause)]
~@body)))
(defvar reenable-paredit-modes
'(emacs-lisp-mode clojure-mode lisp-mode)
"Modes to automatically re-enable paredit for after fixing version-control conflict markers")
(put 'unresolved-conflict 'error-conditions '(unresolved-conflict error))
(put 'unresolved-conflict 'error-message "Unresolved conflict markers in file")
(defun mark-git-conflict-resolved ()
(interactive)
(when (apply #'derived-mode-p reenable-paredit-modes)
(paredit-mode t)) ;; will fail and abort if parens are unbalanced
(let [m '{a {b {c v1, d v2, e v3}, f v4}}]
((fn walk-leaves [parents m]
(lazy-seq
(if-not (map? m)
(list (conj parents m))
(for [[k v] m
leaf (walk-leaves (conj parents k) v)]
leaf))))
[] m))
(defn longest-palindrome [s]
(let [palindrome? #(= (seq %) (reverse %))
slice-all (fn [s] (mapcat #(partition % 1 s)
(map first
(take-while seq
(iterate rest
(range (count s) 1 -1))))))]
(first (filter palindrome? (slice-all s)))))
(defmap AList [entries meta]
IPersistentMap
(count [this]
(count entries))
(valAt [this k not-found]
(if-let [e (find this k)]
(val e)
not-found))
(entryAt [this k]
(first (filter #(= k (key %)) entries)))
(->> (if (and (seq keyseq) (= f adjoin))
(let [[from-id & keys] keyseq]
(for [[to-id edge] (apply edges-map keys (assert-length 1 args))]
((graph/update-in-node incoming [to-id :edges from-id]) read')))
(...))
(apply concat)
(into source-actions))
;; vs