aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Faulhaber <git_net@infolace.com>2009-04-06 00:42:59 +0000
committerTom Faulhaber <git_net@infolace.com>2009-04-06 00:42:59 +0000
commit50e5f78e9bc491f8880d76c086ca4cad28308b41 (patch)
tree3ed4835384c36308ae0a6a12ab7624ab356fc5b5
parent677874c25127d1b94bdabc03b280d8a538beb85f (diff)
Added cl-format examples
-rw-r--r--src/clojure/contrib/pprint/examples/hexdump.clj52
-rw-r--r--src/clojure/contrib/pprint/examples/multiply.clj16
-rw-r--r--src/clojure/contrib/pprint/examples/props.clj17
-rw-r--r--src/clojure/contrib/pprint/examples/show_doc.clj42
4 files changed, 127 insertions, 0 deletions
diff --git a/src/clojure/contrib/pprint/examples/hexdump.clj b/src/clojure/contrib/pprint/examples/hexdump.clj
new file mode 100644
index 00000000..0555b639
--- /dev/null
+++ b/src/clojure/contrib/pprint/examples/hexdump.clj
@@ -0,0 +1,52 @@
+; Copyright (c) Tom Faulhaber, Dec 2008. All rights reserved.
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns clojure.contrib.pprint.examples.hexdump
+ (:use clojure.contrib.pprint)
+ (:gen-class (:main true)))
+
+(def *buffer-length* 1024)
+
+(defn zip-array [base-offset arr]
+ (let [grouped (partition 16 arr)]
+ (first (map-passing-context
+ (fn [line offset]
+ [[offset
+ (map #(if (neg? %) (+ % 256) %) line)
+ (- 16 (count line))
+ (map #(if (<= 32 % 126) (char %) \.) line)]
+ (+ 16 offset)])
+ base-offset grouped))))
+
+
+(defn hexdump
+ ([in-stream] (hexdump in-stream true 0))
+ ([in-stream out-stream] (hexdump [in-stream out-stream 0]))
+ ([in-stream out-stream offset]
+ (let [buf (make-array Byte/TYPE *buffer-length*)]
+ (loop [offset offset
+ count (.read in-stream buf)]
+ (if (neg? count)
+ nil
+ (let [bytes (take count buf)
+ zipped (zip-array offset bytes)]
+ (cl-format out-stream
+ "~:{~8,'0X: ~2{~8@{~#[ ~:;~2,'0X ~]~} ~}~v@{ ~}~2{~8@{~A~} ~}~%~}"
+ zipped)
+ (recur (+ offset *buffer-length*) (.read in-stream buf))))))))
+
+(defn hexdump-file
+ ([file-name] (hexdump-file file-name true))
+ ([file-name stream]
+ (with-open [s (java.io.FileInputStream. file-name)]
+ (hexdump s))))
+
+;; I don't quite understand how to invoke main funcs w/o AOT yet
+(defn -main [& args]
+ (hexdump-file (first args)))
+
diff --git a/src/clojure/contrib/pprint/examples/multiply.clj b/src/clojure/contrib/pprint/examples/multiply.clj
new file mode 100644
index 00000000..f8bfa91a
--- /dev/null
+++ b/src/clojure/contrib/pprint/examples/multiply.clj
@@ -0,0 +1,16 @@
+; Copyright (c) Tom Faulhaber, Dec 2008. All rights reserved.
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns clojure.contrib.pprint.examples.multiply
+ (:use clojure.contrib.pprint))
+
+(defn multiplication-table [limit]
+ (let [nums (range 1 (inc limit))]
+ (cl-format true "~{~{~4d~}~%~}"
+ (map #(map % nums)
+ (map #(partial * %) nums)))))
diff --git a/src/clojure/contrib/pprint/examples/props.clj b/src/clojure/contrib/pprint/examples/props.clj
new file mode 100644
index 00000000..876cedf8
--- /dev/null
+++ b/src/clojure/contrib/pprint/examples/props.clj
@@ -0,0 +1,17 @@
+; Copyright (c) Tom Faulhaber, Dec 2008. All rights reserved.
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns clojure.contrib.pprint.examples.props
+ (:use clojure.contrib.pprint))
+
+(defn show-props [stream]
+ (let [p (mapcat
+ #(vector (key %) (val %))
+ (sort-by key (System/getProperties)))]
+ (cl-format true "~30A~A~%~{~20,,,'-A~10A~}~%~{~30A~S~%~}"
+ "Property" "Value" ["" "" "" ""] p)))
diff --git a/src/clojure/contrib/pprint/examples/show_doc.clj b/src/clojure/contrib/pprint/examples/show_doc.clj
new file mode 100644
index 00000000..e5897ce6
--- /dev/null
+++ b/src/clojure/contrib/pprint/examples/show_doc.clj
@@ -0,0 +1,42 @@
+; Copyright (c) Tom Faulhaber, Dec 2008. All rights reserved.
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns clojure.contrib.pprint.examples.show-doc
+ (:use clojure.contrib.pprint))
+
+(defn ns-list
+ ([] (ns-list nil))
+ ([pattern]
+ (filter
+ (if pattern
+ (comp (partial re-find pattern) name ns-name)
+ (constantly true))
+ (sort-by ns-name (all-ns)))))
+
+(defn show-doc
+ ([] (show-doc nil))
+ ([pattern]
+ (cl-format
+ true
+ "~:{~A: ===============================================~
+ ~%~{~{~a: ~{~a~^, ~}~%~a~%~}~^~%~}~2%~}"
+ (map
+ #(vector (ns-name %)
+ (map
+ (fn [f]
+ (let [f-meta ^(find-var (symbol (str (ns-name %)) (str f)))]
+ [f (:arglists f-meta) (:doc f-meta)]))
+ (filter
+ (fn [a] (instance? clojure.lang.IFn a))
+ (sort (map key (ns-publics %))))))
+ (ns-list pattern)))))
+
+(defn create-api-file [pattern out-file]
+ (with-open [f (java.io.FileWriter. out-file)]
+ (binding [*out* f]
+ (show-doc pattern))))