Permalink
Browse files

now works on clojure 1.2 and 1.3

  • Loading branch information...
jpalmucci committed Sep 7, 2011
1 parent 4e9fee0 commit f55ad330ee329564b38f7068059bed8ce9009558
Showing with 19 additions and 18 deletions.
  1. +3 −3 project.clj
  2. +15 −12 src/yield.clj
  3. +1 −3 test/test.clj
View
@@ -1,6 +1,6 @@
(defproject clj-yield/clj-yield "1.1-SNAPSHOT"
:description "A function like python's yield statement that can push items to a lazy sequence from arbitrary (non-lazy) code."
- :dependencies [[org.clojure/clojure "1.2.0"]
- [org.clojure/clojure-contrib "1.2.0"]
- [clj-iterate/clj-iterate "0.91"]] )
+ :dev-dependencies [[lein-multi "1.1.0-SNAPSHOT"]]
+ :multi-deps {"1.3" [[org.clojure/clojure "1.3.0-beta3"]]
+ "1.2" [[org.clojure/clojure "1.2.1"]]} )
View
@@ -1,5 +1,5 @@
(ns yield
- (:use clojure.test iterate))
+ (:use clojure.test))
;; ********************************************************************************
;;
@@ -14,15 +14,15 @@
;; ********************************************************************************
;; can't put a nil into a linked blocking queue, so use this object to mark them
-(defonce *nil-marker* (Object.))
+(defonce +nil-marker+ (Object.))
;; marker in the BlockingQueue that signals an exception in the
;; generating thread.
(defrecord ExceptionContainer [exception])
;; end of the sequence
-(defonce *end-marker* (Object.))
+(defonce +end-marker+ (Object.))
(defn- offer [queue item]
- (while (not (.offer ^java.util.concurrent.BlockingQueue @queue (if (nil? item) *nil-marker* item) 10 java.util.concurrent.TimeUnit/SECONDS))))
+ (while (not (.offer ^java.util.concurrent.BlockingQueue @queue (if (nil? item) +nil-marker+ item) 10 java.util.concurrent.TimeUnit/SECONDS))))
(defn yield [yseq x]
"Append an element to the sequence 'yseq', created with
@@ -82,16 +82,19 @@ for 'record-blockage'.
(defn with-yielding* [n f pos]
(let [queue (java.util.concurrent.ArrayBlockingQueue. (int n) false)
queue-atom (atom queue)
+ out *out*
+ err *err*
ft (future
- (try
- (f queue-atom)
- (catch Throwable e
- (offer queue-atom (ExceptionContainer. e)))
- (finally (offer queue-atom *end-marker*)
- (reset! queue-atom nil))))
+ (binding [*out* out *err* err]
+ (try
+ (f queue-atom)
+ (catch Throwable e
+ (offer queue-atom (ExceptionContainer. e)))
+ (finally (offer queue-atom +end-marker+)
+ (reset! queue-atom nil)))))
get-ele (fn get-ele [guard]
(let [ele (.take ^java.util.concurrent.BlockingQueue @guard) ]
- (cond (= ele *end-marker*)
+ (cond (= ele +end-marker+)
(do
;; make it impossible for lingering
;; threads to push anything else to the
@@ -105,7 +108,7 @@ for 'record-blockage'.
(.exception ele)))
true
- (cons (if (= ele *nil-marker*) nil ele)
+ (cons (if (= ele +nil-marker+) nil ele)
(lazy-seq (get-ele guard))))))]
(let [guard
(garbage_monitor.
View
@@ -1,7 +1,5 @@
(ns test
- (use yield)
- (use iterate)
- (use clojure.test))
+ (:use yield clojure.test))
(deftest with-yielding-test
;; check that the sequence is correctly generated

0 comments on commit f55ad33

Please sign in to comment.