summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2008-11-19 15:17:30 +0000
committerRich Hickey <richhickey@gmail.com>2008-11-19 15:17:30 +0000
commitfad06a6ae9cff3c55d203253cf5d704975e7edde (patch)
treefc7976fc8ef874dad82bff990d25400bed35385c /src
parentd10f462699a029fa22f6e7fde59af945b5eff9b4 (diff)
enhanced doto now supports functions as well as methods
!! Note - breaking change - you must now use .method instead of just method: (def frame (doto (new JFrame) (add panel) pack show)) becomes: (def frame (doto (new JFrame) (.add panel) .pack .show))
Diffstat (limited to 'src')
-rw-r--r--src/clj/clojure/core.clj17
-rw-r--r--src/clj/clojure/inspector.clj71
2 files changed, 45 insertions, 43 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 1122798f..169a1e5b 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -1765,16 +1765,19 @@
"with-open now requires a vector for its binding"))))
(defmacro doto
- "Evaluates x then calls all of the methods with the supplied
- arguments in succession on the resulting object, returning it.
+ "Evaluates x then calls all of the methods and functions with the
+ value of x supplied at the from of the given arguments. The forms
+ are evaluated in order. Returns x.
- (doto (new java.util.HashMap) (put \"a\" 1) (put \"b\" 2))"
- [x & members]
+ (doto (new java.util.HashMap) (.put \"a\" 1) (.put \"b\" 2))"
+ [x & forms]
(let [gx (gensym)]
`(let [~gx ~x]
- (do
- ~@(map (fn [m] (list '. gx m))
- members))
+ ~@(map (fn [f]
+ (if (seq? f)
+ `(~(first f) ~gx ~@(rest f))
+ `(~f ~gx)))
+ forms)
~gx)))
(defmacro memfn
diff --git a/src/clj/clojure/inspector.clj b/src/clj/clojure/inspector.clj
index 820cd33e..0eae58e7 100644
--- a/src/clj/clojure/inspector.clj
+++ b/src/clj/clojure/inspector.clj
@@ -15,14 +15,13 @@
(javax.swing JPanel JTree JTable JScrollPane JFrame JToolBar JButton SwingUtilities)))
(defn atom? [x]
- (not (instance? clojure.lang.IPersistentCollection x)))
+ (not (coll? x)))
(defn collection-tag [x]
(cond
(instance? java.util.Map$Entry x) :entry
- (instance? clojure.lang.IPersistentMap x) :map
- (instance? java.util.Map x) :map
- (instance? clojure.lang.Sequential x) :seq
+ (instance? java.util.Map x) :map
+ (sequential? x) :seq
:else :atom))
(defmulti is-leaf collection-tag)
@@ -68,13 +67,13 @@
(let [row1 (first data)
colcnt (count row1)
cnt (count data)
- vals (if (instance? clojure.lang.IPersistentMap row1) vals identity)]
+ vals (if (map? row1) vals identity)]
(proxy [TableModel] []
(addTableModelListener [tableModelListener])
(getColumnClass [columnIndex] Object)
(getColumnCount [] colcnt)
(getColumnName [columnIndex]
- (if (instance? clojure.lang.IPersistentMap row1)
+ (if (map? row1)
(name (nth (keys row1) columnIndex))
(str columnIndex)))
(getRowCount [] cnt)
@@ -86,20 +85,20 @@
(defn inspect-tree
"creates a graphical (Swing) inspector on the supplied hierarchical data"
[data]
- (doto (new JFrame "Clojure Inspector")
- (add (new JScrollPane (new JTree (tree-model data))))
- (setSize 400 600)
- (setVisible true)))
+ (doto (JFrame. "Clojure Inspector")
+ (.add (JScrollPane. (JTree. (tree-model data))))
+ (.setSize 400 600)
+ (.setVisible true)))
(defn inspect-table
"creates a graphical (Swing) inspector on the supplied regular
data, which must be a sequential data structure of data structures
of equal length"
[data]
- (doto (new JFrame "Clojure Inspector")
- (add (new JScrollPane (new JTable (old-table-model data))))
- (setSize 400 600)
- (setVisible true)))
+ (doto (JFrame. "Clojure Inspector")
+ (.add (JScrollPane. (JTable. (old-table-model data))))
+ (.setSize 400 600)
+ (.setVisible true)))
(defmulti list-provider class)
@@ -148,27 +147,27 @@
"creates a graphical (Swing) inspector on the supplied object"
[x]
(doto (JFrame. "Clojure Inspector")
- (add
- (doto (JPanel. (BorderLayout.))
- (add (doto (JToolBar.)
- (add (JButton. "Back"))
- (addSeparator)
- (add (JButton. "List"))
- (add (JButton. "Table"))
- (add (JButton. "Bean"))
- (add (JButton. "Line"))
- (add (JButton. "Bar"))
- (addSeparator)
- (add (JButton. "Prev"))
- (add (JButton. "Next")))
- BorderLayout/NORTH)
- (add
- (JScrollPane.
- (doto (JTable. (list-model (list-provider x)))
- (setAutoResizeMode JTable/AUTO_RESIZE_LAST_COLUMN)))
- BorderLayout/CENTER)))
- (setSize 400 400)
- (setVisible true)))
+ (.add
+ (doto (JPanel. (BorderLayout.))
+ (.add (doto (JToolBar.)
+ (.add (JButton. "Back"))
+ (.addSeparator)
+ (.add (JButton. "List"))
+ (.add (JButton. "Table"))
+ (.add (JButton. "Bean"))
+ (.add (JButton. "Line"))
+ (.add (JButton. "Bar"))
+ (.addSeparator)
+ (.add (JButton. "Prev"))
+ (.add (JButton. "Next")))
+ BorderLayout/NORTH)
+ (.add
+ (JScrollPane.
+ (doto (JTable. (list-model (list-provider x)))
+ (.setAutoResizeMode JTable/AUTO_RESIZE_LAST_COLUMN)))
+ BorderLayout/CENTER)))
+ (.setSize 400 400)
+ (.setVisible true)))
(comment
@@ -178,4 +177,4 @@
(inspect-tree {:a 1 :b 2 :c [1 2 3 {:d 4 :e 5 :f [6 7 8]}]})
(inspect-table [[1 2 3][4 5 6][7 8 9][10 11 12]])
-) \ No newline at end of file
+)