Permalink
Browse files

Implement toString for ordered collections

  • Loading branch information...
1 parent 29960de commit e8e99075d25f28abc4294a4e6bdf33f16693a59f @amalloy amalloy committed Apr 13, 2012
Showing with 16 additions and 0 deletions.
  1. +5 −0 src/ordered/map.clj
  2. +3 −0 src/ordered/set.clj
  3. +4 −0 test/ordered/map_test.clj
  4. +4 −0 test/ordered/set_test.clj
View
5 src/ordered/map.clj
@@ -1,6 +1,7 @@
(ns ordered.map
(:use [ordered.common :only [change! Compactable compact]]
[deftype.delegate :only [delegating-deftype]])
+ (:require [clojure.string :as s])
(:import (clojure.lang IPersistentMap
IPersistentCollection
IPersistentVector
@@ -62,6 +63,10 @@
(boolean (seq (filter #(= % v) (.values this)))))
(values [this]
(map (comp val val) (.seq this)))
+
+ Object
+ (toString [this]
+ (str "{" (s/join ", " (for [[k v] this] (str k " " v))) "}"))
(equals [this other]
(.equiv this other))
(hashCode [this]
View
3 src/ordered/set.clj
@@ -1,5 +1,6 @@
(ns ordered.set
(:use [ordered.common :only [Compactable compact change!]])
+ (:require [clojure.string :as s])
(:import (clojure.lang IPersistentSet ITransientSet IEditableCollection
IPersistentMap ITransientMap ITransientAssociative
IPersistentVector ITransientVector
@@ -45,6 +46,8 @@
(vec (.seq this))))
Object
+ (toString [this]
+ (str "#{" (clojure.string/join " " (map str this)) "}"))
(hashCode [this]
(reduce + (map hash (.seq this))))
(equals [this other]
View
4 test/ordered/map_test.clj
@@ -111,6 +111,10 @@
(merge m ())
(into m ())))))
+(deftest object-features
+ (let [m (ordered-map 'a 1 :b 2)]
+ (is (= "{a 1, :b 2}" (str m)))))
+
(deftest transient-support
(let [m (ordered-map {1 2 7 8})]
(testing "Basic transient conj!"
View
4 test/ordered/set_test.clj
@@ -91,6 +91,10 @@
(testing "Ordered disj"
(is (= #{:a 1 2 3} (disj s :b :c))))))
+(deftest object-features
+ (let [s (ordered-set 'a 1 :b 2)]
+ (is (= "#{a 1 :b 2}" (str s)))))
+
(deftest transient-support
(let [s (ordered-set 1 2 7 8)]
(testing "Basic transient conj!"

0 comments on commit e8e9907

Please sign in to comment.