Permalink
Browse files

Added a-finally and some minor tweaks. 0.8.1

  • Loading branch information...
1 parent 9c9f14c commit 2718e80fea9cc385798b52b15729ea0a0fa65590 @duey-sonian duey-sonian committed Apr 1, 2011
Showing with 60 additions and 9 deletions.
  1. +2 −2 project.clj
  2. +31 −3 src/conduit/core.clj
  3. +6 −1 src/conduit/require.clj
  4. +21 −3 test/conduit/test_core.clj
View
@@ -1,6 +1,6 @@
-(defproject conduit "0.8.0"
+(defproject conduit "0.8.1"
:description "Conduit: Stream Processing in Clojure."
- :dependencies [[org.clojure/clojure "1.2.0"]
+ :dependencies [[org.clojure/clojure "1.2.1"]
[org.clojure/clojure-contrib "1.2.0"]
[arrows "1.2.0"]
[swank-clojure "1.2.1"]]
View
@@ -333,14 +333,22 @@
(def a-loop (conduit :a-loop))
(def pass-through
- (a-arr identity))
+ (a-arr identity))
(defn a-selectp [pred & vp-pairs]
(a-comp
(a-all (a-arr pred)
pass-through)
(apply a-select vp-pairs)))
+(defn a-if [a b c]
+ (a-comp (a-all (a-arr (comp boolean a))
+ pass-through)
+ (a-select
+ true b
+ false c)
+ pass-through))
+
(defn a-except [p catch-p]
(letfn [(a-except [f catch-f x]
(try
@@ -359,6 +367,23 @@
:created-by :a-except
:args [p catch-p]}))
+(defn a-finally [p final-p]
+ (letfn [(a-finally [f final-f x]
+ (try
+ (let [[new-x new-f] (f x)]
+ [new-x (partial a-finally new-f final-f)])
+ (finally
+ (final-f x))))]
+ {:parts (:parts p)
+ :reply (partial a-finally
+ (:reply p)
+ (:reply final-p))
+ :no-reply (partial a-finally
+ (:no-reply p)
+ (:no-reply final-p))
+ :created-by :a-finally
+ :args [p final-p]}))
+
(defn conduit-do [p & [v]]
(a-arr (fn [x]
((:no-reply p) x)
@@ -367,8 +392,10 @@
(defn conduit-map [p l]
(if-not (seq l)
(empty l)
- (a-run (comp-fn (:reply (conduit-seq l))
- (:no-reply p)))))
+ (let [result (a-run (comp-fn (:reply (conduit-seq l))
+ (:no-reply p)))]
+ (pr-str result)
+ result)))
(defmacro def-arr [name args & body]
`(def ~name (a-arr (fn ~name ~args ~@body))))
@@ -422,6 +449,7 @@
(a-loop (test-conduit bp)
iv)))
:a-except (apply a-except (map test-conduit (:args p)))
+ :a-finally (apply a-finally (map test-conduit (:args p)))
:disperse (disperse (test-conduit (:args p)))))
(defn test-conduit-fn [p]
View
@@ -1,5 +1,5 @@
(ns conduit.require
- (:use conduit.core)
+ (:use [conduit.core :exclude [disperse test-conduit test-conduit-fn]])
(:refer-clojure :exclude [case comp juxt reduce map]))
(def proc a-arr)
@@ -10,7 +10,12 @@
(def reduce a-loop)
(def map conduit-map)
(def do conduit-do)
+(def if a-if)
(def try a-except)
+(def finally a-finally)
+(def disperse conduit.core/disperse)
+(def test-conduit conduit.core/test-conduit)
+(def test-conduit-fn conduit.core/test-conduit-fn)
(defmacro def [name args & body]
`(def ~name (a-arr (fn ~name ~args ~@body))))
(defmacro def* [name args & body]
View
@@ -439,9 +439,27 @@
(range 5))))
#_(is (= []
- (conduit-map (a-comp (a-all tx tx)
- pass-through)
- (range 5))))))
+ (conduit-map (a-comp (a-all tx tx)
+ pass-through)
+ (range 5))))))
+
+(deftest test-a-finally
+ (let [main-count (atom 0)
+ finally-count (atom 0)
+ te (a-arr (fn [x]
+ (when (even? x)
+ (throw (Exception. "An even int")))
+ (swap! main-count inc)
+ (* 2 x)))
+ tx (a-finally te (a-arr (fn [x]
+ (swap! finally-count inc)
+ x)))
+ tf (a-except tx (a-arr (constantly nil)))]
+ (is (= [nil 2 nil 6 nil]
+ (conduit-map tf (range 5))))
+ (is (= 2 @main-count))
+ (is (= 5 @finally-count))
+))
(deftest test-test-conduit
(def-proc bogus [x]

0 comments on commit 2718e80

Please sign in to comment.