Skip to content

Instantly share code, notes, and snippets.

(ns clj-swing.core)
(import '(javax.swing JFrame JLabel JTextField JButton JComboBox JPanel Timer)
'(java.awt.event ActionListener)
'(java.awt GridBagLayout GridLayout GridBagConstraints))
(require '[clojure.contrib.java-utils :as java])
(defmacro combo-box [[& items] & actions]
`(doto (JComboBox.)
(defun get-window-by-class (class)
"Return a window with the given class."
(first (filter-windows #'(lambda (w) (equal (window-class w) class)))))
(defun gimme (cmdline class)
"If a window matching CLASS is found switch to it, else launch cmdline."
(if class
(let ((wnd (get-window-by-class class)))
(if wnd
(display-window wnd)
;;; do NOT try to print seqs partitioned by the following...
;;; example usage follows code
(defn partition-by* [f r]
(let [igen (fn igen [l s]
(lazy-seq
(if-let [current (first s)]
(if (= l (f current))
(do (swap! r next)
(cons (first s)
;;; from http://fons.github.com/adding-clojure-to-an-existing-slime-setup-in-emacs.html
(defun swank-clojure-autoloads ()
(interactive)
(let ((generated-autoload-file "~/.emacs.d/lang-support/swank-clojure-autoload.el"))
(update-directory-autoloads "~/.emacs.d/lang-support")))
;;; swank-clojure-autoload.el contains
;;; (autoload 'swank-clojure-cmd "swank-clojure" "Initialise Clojure for Swank")
;;; (provide 'swank-clojure-autoload)
We can make this file beautiful and searchable if this error is corrected: Unclosed quoted field in line 10.
"Hot Spots - Method","Self time [%]","Self time","Invocations"
"clj_highlight.syntax.general$re_token__2750$fn__2753.invoke(Object, Object)","21.235117","2250.556 ms","1501200"
"clj_highlight.syntax.general$token__2733$fn__2736.invoke(Object, Object, Object, Object)","19.550106","2071.974 ms","1716400"
"clj_highlight.core$next_token__2687.invoke(Object, Object, Object, Object, Object)","19.44607","2060.948 ms","522400"
"clj_highlight.core$token_seq_STAR___2692$fn__2694.invoke()","12.726099","1348.747 ms","522800"
"clj_highlight.test$_main__2770.invoke()","11.336316","1201.454 ms","100"
"clj_highlight.core$token_seq_STAR___2692.invoke(Object, Object, Object, Object, Object, Object)","5.0202227","532.057 ms","522800"
"clj_highlight.mangler$mangle_tokens__2670$mangle_tokens_STAR___2672$fn__2674.invoke(Object)","4.75099","503.523 ms","522400"
"clj_highlight.syntax.clojure$eval__2757$number_matcher__2759.invoke(Object, Object)","3.0361545","321.78 ms","215200"
"clj_highlight.core$token_seq_STAR___2692$fn__2694.<in
(let [#^java.util.regex.Pattern valid-chars
#"(?i)^[a-z0-9!$&*-_=+<>/.?%][a-z0-9!$&*-_=+<>/.?]*$"]
(defn clj-id? [#^String s]
(if (and (not (zero? (.length s)))
(.. valid-chars (matcher s) matches)
; (== (int -1) (.indexOf s ".."))
(let [slash (.indexOf s (int \/))]
(or (== (int -1) slash)
(== (int -1) (.indexOf s (int \/) (inc slash))))))
true
(def file-info-extractors
{:filename #(.getName #^java.io.File %)
:path #(str (.toURI #^java.io.File %))
:modified #(.lastModified #^java.io.File %)})
(map #(reduce (fn [m [k e]]
(assoc m k (e %)))
{}
file-info-extractors)
(.listFiles (java.io.File. ".")))
(defmacro -> (e &rest es)
(if (and (consp es) (not (consp (cdr es))))
(if (consp (car es))
`(,(caar es) ,e ,@(cdar es))
`(,(car es) ,e))
(if (consp es)
`(-> (-> ,e ,(car es)) ,@(cdr es))
e)))
(defmacro ->> (e &rest es)
;; emacsd-tile.el -- tiling windows for emacs
(defun swap-with (dir)
(interactive)
(let ((other-window (windmove-find-other-window dir)))
(when other-window
(let* ((this-window (selected-window))
(this-buffer (window-buffer this-window))
(other-buffer (window-buffer other-window))
(this-start (window-start this-window))
(defn level-order [f tree]
(loop [to-do [tree]]
(if (empty? to-do)
:done
(do (dorun (map (comp f :value) to-do))
(recur (mapcat (fn [{:keys [left right]}] (remove nil? [left right]))
to-do))))))
(level-order println
{:value 1