Skip to content

Commit

Permalink
Batch of changes for lazier clojure.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chouser committed Feb 17, 2009
1 parent d7695aa commit 81b9e71
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/clojure/contrib/command_line.clj
Expand Up @@ -30,7 +30,7 @@
:else (if-let [found (key-data keybase)]
(if (= \? (last (:sym found)))
(recur r (assoc cmdmap (:sym found) true))
(recur (rest r) (assoc cmdmap (:sym found)
(recur (next r) (assoc cmdmap (:sym found)
(if (or (nil? r) (= \- (ffirst r)))
(:default found)
(first r)))))
Expand Down
6 changes: 3 additions & 3 deletions src/clojure/contrib/error_kit.clj
Expand Up @@ -86,7 +86,7 @@
((:unhandled err) err)
(let [[{:keys [htag] :as handler}] hs]
(if (and htag (not (isa? err-tag htag)))
(recur (rest hs))
(recur (next hs))
(let [rtn ((:hfunc handler) err)]
(if-not (vector? rtn)
(throw-to handler (list rtn))
Expand All @@ -97,7 +97,7 @@
(do (prn *continues*) (throw
(Exception.
(str "Unbound continue name " (rtn 1))))))
::do-not-handle (recur (rest hs))
::do-not-handle (recur (next hs))
(throw-to handler (list rtn)))))))))))

(defmacro raise
Expand Down Expand Up @@ -176,7 +176,7 @@
:when (= (resolve type) #'bind-continue)]
[(list `quote (first more))
`{:blockid '~blockid
:rfunc (fn ~@(rest more))}]))]
:rfunc (fn ~@(next more))}]))]
`(try
(binding [*handler-stack* (list* ~@handlers @#'*handler-stack*)
*continues* (merge @#'*continues* ~@continues)]
Expand Down
39 changes: 21 additions & 18 deletions src/clojure/contrib/lazy_xml.clj
Expand Up @@ -10,7 +10,6 @@

(ns clojure.contrib.lazy-xml
(:require [clojure.xml :as xml])
(:use [clojure.contrib.fcase :only (case)])
(:import (org.xml.sax Attributes InputSource)
(org.xml.sax.helpers DefaultHandler)
(javax.xml.parsers SAXParserFactory)
Expand All @@ -25,7 +24,7 @@
(defn- parse-seq-pull [& _])
(try (load "lazy_xml/with_pull")
(catch Exception e
(when-not (re-seq #"XmlPullParser" (str e))
(when-not (re-find #"XmlPullParser" (str e))
(throw e))))

(defn startparse-sax [s ch]
Expand All @@ -51,9 +50,10 @@
agt (agent nil)
s (if (instance? Reader s) (InputSource. s) s)
step (fn step []
(if-let [x (.take q)]
(lazy-cons x (step))
@agt)) ;will be nil, touch agent just to propagate errors
(lazy-seq
(if-let [x (.take q)]
(cons x (step))
@agt))) ;will be nil, touch agent just to propagate errors
keep-alive (WeakReference. step)
enqueue (fn [x]
(if (.get keep-alive)
Expand Down Expand Up @@ -84,22 +84,25 @@


(defstruct element :tag :attrs :content)
(def mktree)

(defn- siblings
[[event & rst :as s]]
(case (:type event)
:characters (lazy-cons (:str event) (siblings rst))
:start-element (let [t (mktree s)]
(lazy-cons (first t) (siblings (rest t))))
:end-element [rst]))
(declare mktree)

(defn- siblings [coll]
(lazy-seq
(when-let [s (seq coll)]
(let [event (first s)]
(condp = (:type event)
:characters (cons (:str event) (siblings (rest s)))
:start-element (let [t (mktree s)]
(cons (first t) (siblings (rest t))))
:end-element [(rest s)])))))

(defn- mktree
[[elem & events]]
(let [sibs (siblings events)]
(lazy-cons
(struct element (:name elem) (:attrs elem) (drop-last sibs))
(last sibs))))
(lazy-seq
(let [sibs (siblings events)]
(cons
(struct element (:name elem) (:attrs elem) (drop-last sibs))
(last sibs)))))

(defn parse-trim
"Parses the source s, which can be a File, InputStream or String
Expand Down
2 changes: 1 addition & 1 deletion src/clojure/contrib/repl_utils.clj
Expand Up @@ -78,7 +78,7 @@
(:member (nth members selector))
(let [pred (if (ifn? selector)
selector
#(re-seq (re-pattern (str "(?i)" selector)) (:name %)))]
#(re-find (re-pattern (str "(?i)" selector)) (:name %)))]
(println "=== " (Modifier/toString (.getModifiers c)) c " ===")
(doseq [[i m] (indexed members)]
(when (pred m)
Expand Down
4 changes: 2 additions & 2 deletions src/clojure/contrib/shell_out.clj
Expand Up @@ -50,8 +50,8 @@
(if-not args
opts
(if (keyword? arg)
(recur (rrest args) (assoc opts arg (second args)))
(recur (rest args) (update-in opts [:cmd] conj arg))))))
(recur (nnext args) (assoc opts arg (second args)))
(recur (next args) (update-in opts [:cmd] conj arg))))))

(defn- as-env-key [arg]
"Helper so that callers can use symbols, keywords, or strings
Expand Down
8 changes: 4 additions & 4 deletions src/clojure/contrib/zip_filter.clj
Expand Up @@ -23,11 +23,11 @@

(defn right-locs
"Returns a lazy sequence of locations to the right of loc, starting with loc."
[loc] (when loc (lazy-cons (auto false loc) (right-locs (zip/right loc)))))
[loc] (lazy-seq (when loc (cons (auto false loc) (right-locs (zip/right loc))))))

(defn left-locs
"Returns a lazy sequence of locations to the left of loc, starting with loc."
[loc] (when loc (lazy-cons (auto false loc) (left-locs (zip/left loc)))))
[loc] (lazy-seq (when loc (cons (auto false loc) (left-locs (zip/left loc))))))

(defn leftmost?
"Returns true if there are no more nodes to the left of location loc."
Expand Down Expand Up @@ -55,13 +55,13 @@
(defn descendants
"Returns a lazy sequence of all descendants of location loc, in
depth-first order, left-to-right, starting with loc."
[loc] (lazy-cons (auto false loc) (mapcat descendants (children loc))))
[loc] (lazy-seq (cons (auto false loc) (mapcat descendants (children loc)))))

(defn ancestors
"Returns a lazy sequence of all ancestors of location loc, starting
with loc and proceeding to loc's parent node and on through to the
root of the tree."
[loc] (when loc (lazy-cons (auto false loc) (ancestors (zip/up loc)))))
[loc] (lazy-seq (when loc (cons (auto false loc) (ancestors (zip/up loc))))))

(defn- fixup-apply
"Calls (pred loc), and then converts the result to the 'appropriate'
Expand Down
2 changes: 1 addition & 1 deletion src/clojure/contrib/zip_filter/xml.clj
Expand Up @@ -52,7 +52,7 @@
"Returns a query predicate that matches a node when its xml content
matches the query expresions given."
#^{:private true}
[preds] (fn [loc] (and (apply xml-> loc preds) (list loc))))
[preds] (fn [loc] (and (seq (apply xml-> loc preds)) (list loc))))

(defn xml->
"The loc is passed to the first predicate. If the predicate returns
Expand Down

0 comments on commit 81b9e71

Please sign in to comment.