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
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] |
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
(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))) |
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 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]} |
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
(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)))) |
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 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))) |
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
(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 |
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
(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)) |
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 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))))) |
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
(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))) |
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
(->> (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 |