aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonrad Hinsen <konrad.hinsen@laposte.net>2009-03-02 11:35:07 +0000
committerKonrad Hinsen <konrad.hinsen@laposte.net>2009-03-02 11:35:07 +0000
commitadef4d0980992077b804796f80cceb7b5f9ee780 (patch)
treecdd11986ae3ca4174efd43f869e847cba2d7f2bf
parente349bec891f980cad20e0fa734045a6bd57820e2 (diff)
General revision of my modules:
- Replaced clojure.contrib.macros/letfn by clojure.core/letfn - Introduced namespaces for all tests and examples - Fixed namespace-related bugs in monads and stream-utils - Introduced :only clause into all :use clauses
-rw-r--r--src/clojure/contrib/accumulators.clj9
-rw-r--r--src/clojure/contrib/accumulators/examples.clj8
-rw-r--r--src/clojure/contrib/macros.clj11
-rw-r--r--src/clojure/contrib/monads.clj5
-rw-r--r--src/clojure/contrib/monads/examples.clj10
-rw-r--r--src/clojure/contrib/monads/test.clj13
-rw-r--r--src/clojure/contrib/probabilities/dist.clj23
-rw-r--r--src/clojure/contrib/probabilities/dist/examples.clj10
-rw-r--r--src/clojure/contrib/stream_utils.clj43
-rw-r--r--src/clojure/contrib/stream_utils/examples.clj6
-rw-r--r--src/clojure/contrib/types/examples.clj2
11 files changed, 81 insertions, 59 deletions
diff --git a/src/clojure/contrib/accumulators.clj b/src/clojure/contrib/accumulators.clj
index 48b00b2a..f06badf7 100644
--- a/src/clojure/contrib/accumulators.clj
+++ b/src/clojure/contrib/accumulators.clj
@@ -1,7 +1,7 @@
;; Accumulators
;; by Konrad Hinsen
-;; last updated February 27, 2009
+;; last updated March 2, 2009
;; This module defines various accumulators (list, vector, map,
;; sum, product, counter, and combinations thereof) with a common
@@ -20,7 +20,6 @@
(ns clojure.contrib.accumulators
(:use [clojure.contrib.types :only (deftype get-value get-values)])
- (:use [clojure.contrib.macros :only (letfn-kh)])
(:use [clojure.contrib.def :only (defvar defvar- defmacro-)]))
(defmulti add
@@ -222,9 +221,9 @@
(defmethod combine type-tag
[v & vs]
- (letfn-kh [add-item [counter [item n]]
- (assoc counter item (+ n (get counter item 0)))
- add-two [c1 c2] (reduce add-item c1 c2)]
+ (letfn [(add-item [counter [item n]]
+ (assoc counter item (+ n (get counter item 0))))
+ (add-two [c1 c2] (reduce add-item c1 c2))]
(reduce add-two v vs)))
(defmethod add type-tag
diff --git a/src/clojure/contrib/accumulators/examples.clj b/src/clojure/contrib/accumulators/examples.clj
index a7e9f640..ec3c1a01 100644
--- a/src/clojure/contrib/accumulators/examples.clj
+++ b/src/clojure/contrib/accumulators/examples.clj
@@ -6,7 +6,13 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-(use 'clojure.contrib.accumulators)
+(ns clojure.contrib.accumulators.examples
+ (:use [clojure.contrib.accumulators
+ :only (combine add add-items
+ empty-vector empty-list empty-queue empty-set empty-map
+ empty-counter empty-counter-with-total
+ empty-sum empty-product empty-maximum empty-minimum
+ empty-min-max empty-string empty-tuple)]))
; Vector accumulator: combine is concat, add is conj
(combine [:a :b] [:c :d] [:x :y])
diff --git a/src/clojure/contrib/macros.clj b/src/clojure/contrib/macros.clj
index cf4bc1eb..58236fbf 100644
--- a/src/clojure/contrib/macros.clj
+++ b/src/clojure/contrib/macros.clj
@@ -18,9 +18,14 @@
(eval expr))
;; By Konrad Hinsen
-(defmacro letfn-kh
- "A variant of let for local function definitions. fn-bindings consists
- of name/args/body triples, with (letfn-kh [name args body] ...)
+; This macro is made obsolete by Clojure's built-in letfn. I renamed it to
+; letfn- (to avoid a name clash) but leave it in for a while, since its
+; syntax is not quite the same as Clojure's. Expect this to disappear
+; in the long run!
+(defmacro letfn-
+ "OBSOLETE: use clojure.core/letfn
+ A variant of let for local function definitions. fn-bindings consists
+ of name/args/body triples, with (letfn [name args body] ...)
being equivalent to (let [name (fn name args body)] ...)."
[fn-bindings & exprs]
(let [makefn (fn [[name args body]] (list name (list 'fn name args body)))
diff --git a/src/clojure/contrib/monads.clj b/src/clojure/contrib/monads.clj
index b923d026..b27edb46 100644
--- a/src/clojure/contrib/monads.clj
+++ b/src/clojure/contrib/monads.clj
@@ -1,7 +1,7 @@
;; Monads in Clojure
;; by Konrad Hinsen
-;; last updated February 25, 2009
+;; last updated March 2, 2009
;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
@@ -127,9 +127,8 @@
`(defmonadfn ~doc-name ~args ~expr)))
([name args expr]
- (let [fn-name (symbol (format "m+%s+m" (str name)))]
+ (let [fn-name (symbol (str *ns*) (format "m+%s+m" (str name)))]
`(do
- (def ~fn-name nil)
(defmacro ~name ~args
(list (quote ~fn-name)
'~'m-bind '~'m-result '~'m-zero '~'m-plus
diff --git a/src/clojure/contrib/monads/examples.clj b/src/clojure/contrib/monads/examples.clj
index 96632097..2d3dbb12 100644
--- a/src/clojure/contrib/monads/examples.clj
+++ b/src/clojure/contrib/monads/examples.clj
@@ -7,7 +7,14 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(ns clojure.contrib.monads.examples
- (:use clojure.contrib.monads)
+ (:use [clojure.contrib.monads
+ :only (domonad with-monad m-lift m-seq m-when
+ sequence-m
+ maybe-m
+ state-m fetch-state set-state
+ writer-m write
+ cont-m run-cont call-cc
+ maybe-t)])
(:require (clojure.contrib [accumulators :as accu])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -43,7 +50,6 @@
(list x y))
; An example of a sequence function defined in terms of a lift operation.
-; We use m-lift2 because we have to lift a function of two arguments.
(with-monad sequence-m
(defn pairs [xs]
((m-lift 2 #(list %1 %2)) xs xs)))
diff --git a/src/clojure/contrib/monads/test.clj b/src/clojure/contrib/monads/test.clj
index 9fbe3928..8053b30a 100644
--- a/src/clojure/contrib/monads/test.clj
+++ b/src/clojure/contrib/monads/test.clj
@@ -1,7 +1,7 @@
;; Test routines for monads.clj
;; by Konrad Hinsen
-;; last updated February 18, 2009
+;; last updated March 2, 2009
;; Copyright (c) Konrad Hinsen, 2008. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
@@ -11,10 +11,11 @@
;; 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.test-monads
- (:use clojure.contrib.test-is
- clojure.contrib.monads
- clojure.contrib.macros))
+(ns clojure.contrib.monads.test
+ (:use [clojure.contrib.test-is :only (deftest are run-tests)]
+ [clojure.contrib.monads
+ :only (with-monad domonad m-lift m-seq m-chain
+ sequence-m maybe-m maybe-t)]))
(deftest sequence-monad
(with-monad sequence-m
@@ -48,7 +49,7 @@
(deftest seq-maybe-monad
(with-monad (maybe-t sequence-m)
- (letfn-kh [pairs [xs] ((m-lift 2 #(list %1 %2)) xs xs)]
+ (letfn [(pairs [xs] ((m-lift 2 #(list %1 %2)) xs xs))]
(are (= _1 _2)
((m-lift 1 inc) (for [n (range 10)] (when (odd? n) n)))
'(nil 2 nil 4 nil 6 nil 8 nil 10)
diff --git a/src/clojure/contrib/probabilities/dist.clj b/src/clojure/contrib/probabilities/dist.clj
index a5a07c80..21008fc7 100644
--- a/src/clojure/contrib/probabilities/dist.clj
+++ b/src/clojure/contrib/probabilities/dist.clj
@@ -1,7 +1,7 @@
;; Finite probability distributions
;; by Konrad Hinsen
-;; last updated February 18, 2009
+;; last updated March 2, 2009
;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
@@ -12,8 +12,9 @@
;; remove this notice, or any other, from this software.
(ns clojure.contrib.probabilities.dist
- (:use clojure.contrib.monads clojure.contrib.macros
- clojure.contrib.monads clojure.contrib.def))
+ (:use [clojure.contrib.monads
+ :only (defmonad domonad with-monad maybe-t m-lift m-chain)]
+ [clojure.contrib.def :only (defvar)]))
; The probability distribution monad. It is limited to finite probability
; distributions (e.g. there is a finite number of possible value), which
@@ -26,8 +27,8 @@
[m-result (fn m-result-dist [v]
{v 1})
m-bind (fn m-bind-dist [mv f]
- (letfn-kh [add-prob [dist [x p]]
- (assoc dist x (+ (get dist x 0) p))]
+ (letfn [(add-prob [dist [x p]]
+ (assoc dist x (+ (get dist x 0) p)))]
(reduce add-prob {}
(for [[x p] mv [y q] (f x)]
[y (* q p)]))))
@@ -91,12 +92,12 @@
pairs. In the last pair, the probability can be given by the keyword
:else, which stands for 1 minus the total of the other probabilities."
[& choices]
- (letfn-kh [add-choice [dist [p v]]
+ (letfn [(add-choice [dist [p v]]
(cond (nil? p) dist
(= p :else)
- (let [total-p (reduce + (vals dist))]
- (assoc dist v (- 1 total-p)))
- :else (assoc dist v p))]
+ (let [total-p (reduce + (vals dist))]
+ (assoc dist v (- 1 total-p)))
+ :else (assoc dist v p)))]
(reduce add-choice {} (partition 2 choices))))
(with-monad dist-m
@@ -132,10 +133,10 @@
(with-monad dist-m
(defn- select-n [n xs]
- (letfn-kh [select-1 [[s xs]]
+ (letfn [(select-1 [[s xs]]
(uniform (for [i (range (count xs))]
(let [[nth rest] (nth-and-rest i xs)]
- (list (cons nth s) rest))))]
+ (list (cons nth s) rest)))))]
((m-chain (replicate n select-1)) (list '() xs))))
(defn select [n xs]
diff --git a/src/clojure/contrib/probabilities/dist/examples.clj b/src/clojure/contrib/probabilities/dist/examples.clj
index 947f5366..17bf401e 100644
--- a/src/clojure/contrib/probabilities/dist/examples.clj
+++ b/src/clojure/contrib/probabilities/dist/examples.clj
@@ -6,9 +6,13 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-(use 'clojure.contrib.probabilities.dist
- 'clojure.contrib.monads)
-(require 'clojure.contrib.accumulators)
+(ns clojure.contrib.probabilities.dist.examples
+ (:use [clojure.contrib.probabilities.dist
+ :only (uniform prob cond-prob join-with dist-m choose
+ normalize certainly cond-dist-m normalize-cond)])
+ (:use [clojure.contrib.monads
+ :only (domonad with-monad m-seq m-chain m-lift)])
+ (:require clojure.contrib.accumulators))
;; Simple examples using dice
diff --git a/src/clojure/contrib/stream_utils.clj b/src/clojure/contrib/stream_utils.clj
index 3ba04e69..c2bdc101 100644
--- a/src/clojure/contrib/stream_utils.clj
+++ b/src/clojure/contrib/stream_utils.clj
@@ -1,7 +1,7 @@
;; Stream utilities
;; by Konrad Hinsen
-;; last updated February 23, 2009
+;; last updated March 2, 2009
;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
@@ -52,8 +52,7 @@
whereas pick-all returns the next value of all stream arguments
in the form of a vector."
- (:use [clojure.contrib.monads])
- (:use [clojure.contrib.macros :only (letfn-kh)]))
+ (:use [clojure.contrib.monads :only (defmonad with-monad)]))
(let [eos (Object.)
@@ -150,13 +149,13 @@
stream-m monad) and a vector of stream arguments and returns a stream
generator function representing the output stream of the transformer."
[st streams]
- (letfn-kh [make-gen [s]
- (fn [eos]
- (loop [s s]
- (let [[v ns] (st s)]
- (cond (stream-eos? v) [eos nil]
- (stream-skip? v) (recur ns)
- :else [v (make-gen ns)]))))]
+ (letfn [(make-gen [s]
+ (fn [eos]
+ (loop [s s]
+ (let [[v ns] (st s)]
+ (cond (stream-eos? v) [eos nil]
+ (stream-skip? v) (recur ns)
+ :else [v (make-gen ns)])))))]
(make-gen streams)))
)
@@ -176,14 +175,14 @@
The non-stream arguments args and the stream arguments streams
are given separately, with args being possibly empty."
[name args streams & body]
- (defst 'st-as-seq name args streams body))
+ (defst `st-as-seq name args streams body))
(defmacro defst-gen
"Define the generator-returning stream transformer name by body.
The non-stream arguments args and the stream arguments streams
are given separately, with args being possibly empty."
[name args streams & body]
- (defst 'st-as-generator name args streams body))
+ (defst `st-as-generator name args streams body))
(defn stream-drop
"Return a stream containing all but the first n elements of stream."
@@ -207,15 +206,15 @@
sequences. Flattening is not recursive, only one level of sequences
will be removed."
[s]
- (letfn-kh [buffer-gen [buffer stream]
- (fn [eos]
- (loop [buffer buffer
- stream stream]
- (if (nil? buffer)
- (let [[v new-stream] (stream-next stream)]
- (cond (stream-eos? v) [eos nil]
- (empty? v) (recur nil new-stream)
- :else (recur v new-stream)))
- [(first buffer) (buffer-gen (next buffer) stream)])))]
+ (letfn [(buffer-gen [buffer stream]
+ (fn [eos]
+ (loop [buffer buffer
+ stream stream]
+ (if (nil? buffer)
+ (let [[v new-stream] (stream-next stream)]
+ (cond (stream-eos? v) [eos nil]
+ (empty? v) (recur nil new-stream)
+ :else (recur v new-stream)))
+ [(first buffer) (buffer-gen (next buffer) stream)]))))]
(buffer-gen nil s)))
diff --git a/src/clojure/contrib/stream_utils/examples.clj b/src/clojure/contrib/stream_utils/examples.clj
index 0d0454a7..cc19f2c4 100644
--- a/src/clojure/contrib/stream_utils/examples.clj
+++ b/src/clojure/contrib/stream_utils/examples.clj
@@ -6,8 +6,10 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-(use 'clojure.contrib.stream-utils)
-(use 'clojure.contrib.monads)
+(ns clojure.contrib.stream-utils.examples
+ (:use [clojure.contrib.stream-utils
+ :only (defst-seq defst-gen pick pick-all stream-as-seq)])
+ (:use [clojure.contrib.monads :only (domonad)]))
; Transform a stream of numbers into a stream of sums of
; two consecutive numbers.
diff --git a/src/clojure/contrib/types/examples.clj b/src/clojure/contrib/types/examples.clj
index 4e801f75..5a39938f 100644
--- a/src/clojure/contrib/types/examples.clj
+++ b/src/clojure/contrib/types/examples.clj
@@ -7,7 +7,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(ns clojure.contrib.types.examples
- (:use clojure.contrib.types))
+ (:use [clojure.contrib.types :only (deftype match get-value get-values)]))
;
; A simple tree structure