From 8bfdb774216486d3066d01beeb32f6c90e46fd86 Mon Sep 17 00:00:00 2001
From: Mark Engelberg <mark.engelberg@gmail.com>
Date: Sat, 17 Jul 2010 02:32:45 -0700
Subject: [PATCH] Changed sorted's seqFrom to take an additional inclusive boolean parameter

---
 src/clj/clojure/core.clj                    |   32 +++++++++++++-------------
 src/jvm/clojure/lang/PersistentTreeMap.java |   16 ++++++++++---
 src/jvm/clojure/lang/PersistentTreeSet.java |    4 +-
 src/jvm/clojure/lang/Sorted.java            |    2 +-
 4 files changed, 31 insertions(+), 23 deletions(-)

diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index ade4405..1131856 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -4029,15 +4029,15 @@
   which (test (.. sc comparator (compare ek key)) 0) is true"
   {:added "1.0"}
   ([^clojure.lang.Sorted sc test key]
-   (let [include (mk-bound-fn sc test key)]
-     (if (#{> >=} test)
-       (when-let [[e :as s] (. sc seqFrom key true)]
-         (if (include e) s (next s)))
-       (take-while include (. sc seq true)))))
+   (cond
+    (= test >) (. sc seqFrom key true false)
+    (= test >=) (. sc seqFrom key true true)
+    :else (take-while (mk-bound-fn sc test key) (. sc seq true))))
   ([^clojure.lang.Sorted sc start-test start-key end-test end-key]
-   (when-let [[e :as s] (. sc seqFrom start-key true)]
-     (take-while (mk-bound-fn sc end-test end-key)
-                 (if ((mk-bound-fn sc start-test start-key) e) s (next s))))))
+   (when-let [s (if (= start-test >)
+		  (. sc seqFrom start-key true false)
+		  (. sc seqFrom start-key true true))]
+     (take-while (mk-bound-fn sc end-test end-key) s))))
 
 (defn rsubseq
   "sc must be a sorted collection, test(s) one of <, <=, > or
@@ -4045,15 +4045,15 @@
   which (test (.. sc comparator (compare ek key)) 0) is true"
   {:added "1.0"}
   ([^clojure.lang.Sorted sc test key]
-   (let [include (mk-bound-fn sc test key)]
-     (if (#{< <=} test)
-       (when-let [[e :as s] (. sc seqFrom key false)]
-         (if (include e) s (next s)))
-       (take-while include (. sc seq false)))))
+   (cond
+    (= test <) (. sc seqFrom key false false)
+    (= test <=) (. sc seqFrom key false true)
+    :else (take-while (mk-bound-fn sc test key) (. sc seq false))))
   ([^clojure.lang.Sorted sc start-test start-key end-test end-key]
-   (when-let [[e :as s] (. sc seqFrom end-key false)]
-     (take-while (mk-bound-fn sc start-test start-key)
-                 (if ((mk-bound-fn sc end-test end-key) e) s (next s))))))
+   (when-let [s (if (= end-test <)
+		  (. sc seqFrom end-key false false)
+		  (. sc seqFrom end-key false true))]
+     (take-while (mk-bound-fn sc start-test start-key) s))))
 
 (defn repeatedly
   "Takes a function of no args, presumably with side effects, and
diff --git a/src/jvm/clojure/lang/PersistentTreeMap.java b/src/jvm/clojure/lang/PersistentTreeMap.java
index 4fd987f..6cad191 100644
--- a/src/jvm/clojure/lang/PersistentTreeMap.java
+++ b/src/jvm/clojure/lang/PersistentTreeMap.java
@@ -161,7 +161,7 @@ public ISeq seq(boolean ascending){
 	return null;
 }
 
-public ISeq seqFrom(Object key, boolean ascending){
+public ISeq seqFrom(Object key, boolean ascending, boolean inclusive){
 	if(_count > 0)
 		{
 		ISeq stack = null;
@@ -172,7 +172,10 @@ public ISeq seqFrom(Object key, boolean ascending){
 			if(c == 0)
 				{
 				stack = RT.cons(t, stack);
-				return new Seq(stack, ascending);
+				if (inclusive)
+					return new Seq(stack, ascending);
+				else
+					return (new Seq(stack, ascending)).next();
 				}
 			else if(ascending)
 				{
@@ -194,9 +197,14 @@ public ISeq seqFrom(Object key, boolean ascending){
 				else
 					t = t.left();
 				}
-			}
+			}		
 		if(stack != null)
-			return new Seq(stack, ascending);
+			{
+			if (inclusive)
+				return new Seq(stack, ascending);
+			else
+				return (new Seq(stack, ascending)).next();
+			}
 		}
 	return null;
 }
diff --git a/src/jvm/clojure/lang/PersistentTreeSet.java b/src/jvm/clojure/lang/PersistentTreeSet.java
index d55452c..b38147e 100644
--- a/src/jvm/clojure/lang/PersistentTreeSet.java
+++ b/src/jvm/clojure/lang/PersistentTreeSet.java
@@ -79,9 +79,9 @@ public ISeq seq(boolean ascending){
 	return RT.keys(m.seq(ascending));
 }
 
-public ISeq seqFrom(Object key, boolean ascending){
+public ISeq seqFrom(Object key, boolean ascending, boolean inclusive){
 	PersistentTreeMap m = (PersistentTreeMap) impl;
-	return RT.keys(m.seqFrom(key,ascending));
+	return RT.keys(m.seqFrom(key,ascending,inclusive));
 }
 
 public IPersistentMap meta(){
diff --git a/src/jvm/clojure/lang/Sorted.java b/src/jvm/clojure/lang/Sorted.java
index dc96dd9..600df3f 100644
--- a/src/jvm/clojure/lang/Sorted.java
+++ b/src/jvm/clojure/lang/Sorted.java
@@ -21,5 +21,5 @@ Object entryKey(Object entry);
 
 ISeq seq(boolean ascending);
 
-ISeq seqFrom(Object key, boolean ascending);
+ISeq seqFrom(Object key, boolean ascending, boolean inclusive);
 }
-- 
1.6.4.msysgit.0

