summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2009-02-16 18:53:12 +0000
committerRich Hickey <richhickey@gmail.com>2009-02-16 18:53:12 +0000
commit2a4f2a2bb19dc13d93130061bf99c2b73acffe66 (patch)
tree48b3e7d5eb0334bb81207374c354ce61b3f2f283
parent20331a1681ebc7ba714d0e55563cb51d143c1afc (diff)
[lazy] got rid of Sequence, (seq iseq) no longer an identity, ISeqs can be empty, PersistentList.EMPTY and LazySeq.EMPTY are ISeqs, rest returns ISeq
-rw-r--r--src/clj/clojure/core.clj21
-rw-r--r--src/clj/clojure/core_print.clj16
-rw-r--r--src/jvm/clojure/lang/ASeq.java2
-rw-r--r--src/jvm/clojure/lang/Cons.java10
-rw-r--r--src/jvm/clojure/lang/Delay.java348
-rw-r--r--src/jvm/clojure/lang/ISeq.java4
-rw-r--r--src/jvm/clojure/lang/LazySeq.java16
-rw-r--r--src/jvm/clojure/lang/LispReader.java2
-rw-r--r--src/jvm/clojure/lang/PersistentList.java16
-rw-r--r--src/jvm/clojure/lang/RT.java34
-rw-r--r--src/jvm/clojure/lang/Sequence.java16
11 files changed, 244 insertions, 241 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 1c164fe3..63c0f870 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -96,7 +96,7 @@
(def
#^{:arglists '([coll])
- :doc "Sequence. Returns a new ISeq on the collection. If the
+ :doc "Returns a new ISeq on the collection. If the
collection is empty, returns nil. (seq nil) returns nil. seq also
works on Strings, native Java arrays (of reference types) and any
objects that implement Iterable."
@@ -116,11 +116,6 @@
(def
#^{:arglists '([x])
- :doc "Return true if x implements Sequence, true of ISeqs and LazySeqs"}
- sequence? (fn sequence? [x] (instance? clojure.lang.Sequence x)))
-
-(def
- #^{:arglists '([x])
:doc "Return true if x is a String"}
string? (fn string? [x] (instance? String x)))
@@ -138,7 +133,7 @@
#^{:private true}
sigs
(fn [fdecl]
- (if (sequence? (first fdecl))
+ (if (seq? (first fdecl))
(loop [ret [] fdecl fdecl]
(if fdecl
(recur (conj ret (first (first fdecl))) (next fdecl))
@@ -185,7 +180,7 @@
(def
#^{:arglists '([coll])
- :doc "Return a sequence of all but the last item in coll, in linear time"}
+ :doc "Return a seq of all but the last item in coll, in linear time"}
butlast (fn butlast [s]
(loop [ret [] s s]
(if (next s)
@@ -430,7 +425,7 @@
(list* '#^{:once true :super-name "clojure/lang/LazySeq"} fn* [] body))
(defn concat
- "Returns a lazy sequence representing the concatenation of the elements in the supplied colls."
+ "Returns a lazy seq representing the concatenation of the elements in the supplied colls."
([] (lazy-seq nil))
([x] (lazy-seq x))
([x y]
@@ -971,8 +966,8 @@
(. e (getValue)))
(defn rseq
- "Returns, in constant time, a sequence of the items in rev (which
- can be a vector or sorted-map), in reverse order."
+ "Returns, in constant time, a seq of the items in rev (which
+ can be a vector or sorted-map), in reverse order. If rev is empty returns nil"
[#^clojure.lang.Reversible rev]
(. rev (rseq)))
@@ -1021,7 +1016,7 @@
second item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc."
- ([x form] (if (sequence? form)
+ ([x form] (if (seq? form)
`(~(first form) ~x ~@(next form))
(list form x)))
([x form & more] `(-> (-> ~x ~form) ~@more)))
@@ -2080,7 +2075,7 @@
(let [gx (gensym)]
`(let [~gx ~x]
~@(map (fn [f]
- (if (sequence? f)
+ (if (seq? f)
`(~(first f) ~gx ~@(next f))
`(~f ~gx)))
forms)
diff --git a/src/clj/clojure/core_print.clj b/src/clj/clojure/core_print.clj
index b2589f6f..ce806074 100644
--- a/src/clj/clojure/core_print.clj
+++ b/src/clj/clojure/core_print.clj
@@ -128,18 +128,18 @@
(defmethod print-dup clojure.lang.Var [#^clojure.lang.Var o, #^Writer w]
(.write w (str "#=(var " (.name (.ns o)) "/" (.sym o) ")")))
-(defmethod print-method clojure.lang.Sequence [o, #^Writer w]
+(defmethod print-method clojure.lang.ISeq [o, #^Writer w]
(print-meta o w)
(print-sequential "(" pr-on " " ")" o w))
-(defmethod print-dup clojure.lang.Sequence [o w] (print-method o w))
+(defmethod print-dup clojure.lang.ISeq [o w] (print-method o w))
(defmethod print-dup clojure.lang.IPersistentList [o w] (print-method o w))
-(prefer-method print-method clojure.lang.IPersistentList clojure.lang.Sequence)
-(prefer-method print-dup clojure.lang.IPersistentList clojure.lang.Sequence)
-(prefer-method print-method clojure.lang.Sequence clojure.lang.IPersistentCollection)
-(prefer-method print-dup clojure.lang.Sequence clojure.lang.IPersistentCollection)
-(prefer-method print-method clojure.lang.Sequence java.util.Collection)
-(prefer-method print-dup clojure.lang.Sequence java.util.Collection)
+(prefer-method print-method clojure.lang.IPersistentList clojure.lang.ISeq)
+(prefer-method print-dup clojure.lang.IPersistentList clojure.lang.ISeq)
+(prefer-method print-method clojure.lang.ISeq clojure.lang.IPersistentCollection)
+(prefer-method print-dup clojure.lang.ISeq clojure.lang.IPersistentCollection)
+(prefer-method print-method clojure.lang.ISeq java.util.Collection)
+(prefer-method print-dup clojure.lang.ISeq java.util.Collection)
(defmethod print-method clojure.lang.IPersistentList [o, #^Writer w]
(print-meta o w)
diff --git a/src/jvm/clojure/lang/ASeq.java b/src/jvm/clojure/lang/ASeq.java
index fdd41c78..49ff5b6d 100644
--- a/src/jvm/clojure/lang/ASeq.java
+++ b/src/jvm/clojure/lang/ASeq.java
@@ -110,7 +110,7 @@ public ISeq cons(Object o){
return new Cons(o, this);
}
-public Sequence more(){
+public ISeq more(){
ISeq s = next();
if(s == null)
return LazySeq.EMPTY;
diff --git a/src/jvm/clojure/lang/Cons.java b/src/jvm/clojure/lang/Cons.java
index 9606094e..8425865c 100644
--- a/src/jvm/clojure/lang/Cons.java
+++ b/src/jvm/clojure/lang/Cons.java
@@ -12,18 +12,18 @@
package clojure.lang;
-public class Cons extends ASeq{
+final public class Cons extends ASeq{
private final Object _first;
-private final Sequence _more;
+private final ISeq _more;
-public Cons(Object first, Sequence _more){
+public Cons(Object first, ISeq _more){
this._first = first;
this._more = _more;
}
-public Cons(IPersistentMap meta, Object _first, Sequence _more){
+public Cons(IPersistentMap meta, Object _first, ISeq _more){
super(meta);
this._first = _first;
this._more = _more;
@@ -37,7 +37,7 @@ public ISeq next(){
return more().seq();
}
-public Sequence more(){
+public ISeq more(){
if(_more == null)
return LazySeq.EMPTY;
return _more;
diff --git a/src/jvm/clojure/lang/Delay.java b/src/jvm/clojure/lang/Delay.java
index cfaad693..d0bf38de 100644
--- a/src/jvm/clojure/lang/Delay.java
+++ b/src/jvm/clojure/lang/Delay.java
@@ -38,178 +38,178 @@ public class Delay {
return val;
}
- static public class Seq extends Delay implements List, Sequence {
- public Seq(IFn fn) {
- super(fn);
- }
-
- public ISeq seq() {
- try
- {
- return RT.seq(get());
- }
- catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
-
- public int count() {
- int c = 0;
- for (ISeq s = seq(); s != null; s = s.next())
- ++c;
- return c;
- }
-
- public IPersistentCollection cons(Object o) {
- return RT.cons(o, seq());
- }
-
- public IPersistentCollection empty() {
- return null;
- }
-
- public boolean equiv(Object o) {
- ISeq s = seq();
- return s == o || (s != null && s.equiv(o));
- }
-
- public int hashCode() {
- return Util.hash(seq());
- }
-
- public boolean equals(Object o) {
- ISeq s = seq();
- return s == o || (s != null && s.equals(o));
- }
-
-
-// java.util.Collection implementation
-
- public Object[] toArray() {
- return RT.seqToArray(seq());
- }
-
- public boolean add(Object o) {
- throw new UnsupportedOperationException();
- }
-
- public boolean remove(Object o) {
- throw new UnsupportedOperationException();
- }
-
- public boolean addAll(Collection c) {
- throw new UnsupportedOperationException();
- }
-
- public void clear() {
- throw new UnsupportedOperationException();
- }
-
- public boolean retainAll(Collection c) {
- throw new UnsupportedOperationException();
- }
-
- public boolean removeAll(Collection c) {
- throw new UnsupportedOperationException();
- }
-
- public boolean containsAll(Collection c) {
- for (Object o : c)
- {
- if (!contains(o))
- return false;
- }
- return true;
- }
-
- public Object[] toArray(Object[] a) {
- if (a.length >= count())
- {
- ISeq s = seq();
- for (int i = 0; s != null; ++i, s = s.next())
- {
- a[i] = s.first();
- }
- if (a.length > count())
- a[count()] = null;
- return a;
- }
- else
- return toArray();
- }
-
- public int size() {
- return count();
- }
-
- public boolean isEmpty() {
- return count() == 0;
- }
-
- public boolean contains(Object o) {
- for (ISeq s = seq(); s != null; s = s.next())
- {
- if (Util.equiv(s.first(), o))
- return true;
- }
- return false;
- }
-
- public Iterator iterator() {
- return new SeqIterator(seq());
- }
-
- //////////// List stuff /////////////////
- private List reify() {
- return new ArrayList(this);
- }
-
- public List subList(int fromIndex, int toIndex) {
- return reify().subList(fromIndex, toIndex);
- }
-
- public Object set(int index, Object element) {
- throw new UnsupportedOperationException();
- }
-
- public Object remove(int index) {
- throw new UnsupportedOperationException();
- }
-
- public int indexOf(Object o) {
- ISeq s = seq();
- for (int i = 0; s != null; s = s.next(), i++)
- {
- if (Util.equiv(s.first(), o))
- return i;
- }
- return -1;
- }
-
- public int lastIndexOf(Object o) {
- return reify().lastIndexOf(o);
- }
-
- public ListIterator listIterator() {
- return reify().listIterator();
- }
-
- public ListIterator listIterator(int index) {
- return reify().listIterator(index);
- }
-
- public Object get(int index) {
- return RT.nth(this, index);
- }
-
- public void add(int index, Object element) {
- throw new UnsupportedOperationException();
- }
-
- public boolean addAll(int index, Collection c) {
- throw new UnsupportedOperationException();
- }
-
- }
+// static public class Seq extends Delay implements List, ISeq {
+// public Seq(IFn fn) {
+// super(fn);
+// }
+//
+// public ISeq seq() {
+// try
+// {
+// return RT.seq(get());
+// }
+// catch (Exception e)
+// {
+// throw new RuntimeException(e);
+// }
+// }
+//
+// public int count() {
+// int c = 0;
+// for (ISeq s = seq(); s != null; s = s.next())
+// ++c;
+// return c;
+// }
+//
+// public IPersistentCollection cons(Object o) {
+// return RT.cons(o, seq());
+// }
+//
+// public IPersistentCollection empty() {
+// return null;
+// }
+//
+// public boolean equiv(Object o) {
+// ISeq s = seq();
+// return s == o || (s != null && s.equiv(o));
+// }
+//
+// public int hashCode() {
+// return Util.hash(seq());
+// }
+//
+// public boolean equals(Object o) {
+// ISeq s = seq();
+// return s == o || (s != null && s.equals(o));
+// }
+//
+//
+//// java.util.Collection implementation
+//
+// public Object[] toArray() {
+// return RT.seqToArray(seq());
+// }
+//
+// public boolean add(Object o) {
+// throw new UnsupportedOperationException();
+// }
+//
+// public boolean remove(Object o) {
+// throw new UnsupportedOperationException();
+// }
+//
+// public boolean addAll(Collection c) {
+// throw new UnsupportedOperationException();
+// }
+//
+// public void clear() {
+// throw new UnsupportedOperationException();
+// }
+//
+// public boolean retainAll(Collection c) {
+// throw new UnsupportedOperationException();
+// }
+//
+// public boolean removeAll(Collection c) {
+// throw new UnsupportedOperationException();
+// }
+//
+// public boolean containsAll(Collection c) {
+// for (Object o : c)
+// {
+// if (!contains(o))
+// return false;
+// }
+// return true;
+// }
+//
+// public Object[] toArray(Object[] a) {
+// if (a.length >= count())
+// {
+// ISeq s = seq();
+// for (int i = 0; s != null; ++i, s = s.next())
+// {
+// a[i] = s.first();
+// }
+// if (a.length > count())
+// a[count()] = null;
+// return a;
+// }
+// else
+// return toArray();
+// }
+//
+// public int size() {
+// return count();
+// }
+//
+// public boolean isEmpty() {
+// return count() == 0;
+// }
+//
+// public boolean contains(Object o) {
+// for (ISeq s = seq(); s != null; s = s.next())
+// {
+// if (Util.equiv(s.first(), o))
+// return true;
+// }
+// return false;
+// }
+//
+// public Iterator iterator() {
+// return new SeqIterator(seq());
+// }
+//
+// //////////// List stuff /////////////////
+// private List reify() {
+// return new ArrayList(this);
+// }
+//
+// public List subList(int fromIndex, int toIndex) {
+// return reify().subList(fromIndex, toIndex);
+// }
+//
+// public Object set(int index, Object element) {
+// throw new UnsupportedOperationException();
+// }
+//
+// public Object remove(int index) {
+// throw new UnsupportedOperationException();
+// }
+//
+// public int indexOf(Object o) {
+// ISeq s = seq();
+// for (int i = 0; s != null; s = s.next(), i++)
+// {
+// if (Util.equiv(s.first(), o))
+// return i;
+// }
+// return -1;
+// }
+//
+// public int lastIndexOf(Object o) {
+// return reify().lastIndexOf(o);
+// }
+//
+// public ListIterator listIterator() {
+// return reify().listIterator();
+// }
+//
+// public ListIterator listIterator(int index) {
+// return reify().listIterator(index);
+// }
+//
+// public Object get(int index) {
+// return RT.nth(this, index);
+// }
+//
+// public void add(int index, Object element) {
+// throw new UnsupportedOperationException();
+// }
+//
+// public boolean addAll(int index, Collection c) {
+// throw new UnsupportedOperationException();
+// }
+//
+// }
}
diff --git a/src/jvm/clojure/lang/ISeq.java b/src/jvm/clojure/lang/ISeq.java
index 6637b0bf..6edb5191 100644
--- a/src/jvm/clojure/lang/ISeq.java
+++ b/src/jvm/clojure/lang/ISeq.java
@@ -16,13 +16,13 @@ package clojure.lang;
* ISeqs are immutable values, i.e. neither first(), nor rest() changes
* or invalidates the ISeq
*/
-public interface ISeq extends Sequence{
+public interface ISeq extends IPersistentCollection, Sequential{
Object first();
ISeq next();
-Sequence more();
+ISeq more();
ISeq cons(Object o);
diff --git a/src/jvm/clojure/lang/LazySeq.java b/src/jvm/clojure/lang/LazySeq.java
index 05d8f030..64be9ed0 100644
--- a/src/jvm/clojure/lang/LazySeq.java
+++ b/src/jvm/clojure/lang/LazySeq.java
@@ -14,7 +14,7 @@ package clojure.lang;
import java.util.*;
-public class LazySeq extends AFn implements List, Sequence {
+public class LazySeq extends AFn implements ISeq, List {
static final ISeq DUMMY = new Cons(null, null);
static final LazySeq EMPTY = new LazySeq(null);
@@ -50,7 +50,19 @@ public class LazySeq extends AFn implements List, Sequence {
return c;
}
- public IPersistentCollection cons(Object o) {
+ public Object first() {
+ return RT.first(seq());
+ }
+
+ public ISeq next() {
+ return RT.next(seq());
+ }
+
+ public ISeq more() {
+ return RT.more(seq());
+ }
+
+ public ISeq cons(Object o) {
return RT.cons(o, seq());
}
diff --git a/src/jvm/clojure/lang/LispReader.java b/src/jvm/clojure/lang/LispReader.java
index ecca2b32..8a733c5f 100644
--- a/src/jvm/clojure/lang/LispReader.java
+++ b/src/jvm/clojure/lang/LispReader.java
@@ -713,7 +713,7 @@ public static class SyntaxQuoteReader extends AFn{
{
ret = RT.list(APPLY, HASHSET, RT.list(SEQ, RT.cons(CONCAT, sqExpandList(((IPersistentSet) form).seq()))));
}
- else if(form instanceof Sequence)
+ else if(form instanceof ISeq)
{
ISeq seq = RT.seq(form);
if(seq == null)
diff --git a/src/jvm/clojure/lang/PersistentList.java b/src/jvm/clojure/lang/PersistentList.java
index f6efd69a..f98c567f 100644
--- a/src/jvm/clojure/lang/PersistentList.java
+++ b/src/jvm/clojure/lang/PersistentList.java
@@ -113,7 +113,7 @@ public Object reduce(IFn f, Object start) throws Exception{
}
- static class EmptyList extends Obj implements IPersistentList, List, Sequence{
+ static class EmptyList extends Obj implements IPersistentList, List, ISeq{
public int hashCode(){
return 1;
@@ -131,7 +131,19 @@ public Object reduce(IFn f, Object start) throws Exception{
super(meta);
}
- public PersistentList cons(Object o){
+ public Object first() {
+ return null;
+ }
+
+ public ISeq next() {
+ return null;
+ }
+
+ public ISeq more() {
+ return this;
+ }
+
+ public PersistentList cons(Object o){
return new PersistentList(meta(), o, null, 1);
}
diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java
index 1c8b30f5..ba6dc783 100644
--- a/src/jvm/clojure/lang/RT.java
+++ b/src/jvm/clojure/lang/RT.java
@@ -445,22 +445,20 @@ static public int nextID(){
static public ISeq seq(Object coll){
if(coll == null)
return null;
- else if(coll instanceof ISeq)
- return (ISeq) coll;
- else if(coll instanceof Seqable)
- return ((Seqable) coll).seq();
+ else if(coll instanceof ASeq)
+ return (ASeq) coll;
else
return seqFrom(coll);
}
-static public Sequence sequence(Object coll){
- if(coll == null)
- return null;
- else if(coll instanceof Sequence)
- return (Sequence) coll;
- else
- return seq(coll);
- }
+//static public Sequence sequence(Object coll){
+// if(coll == null)
+// return null;
+// else if(coll instanceof Sequence)
+// return (Sequence) coll;
+// else
+// return seq(coll);
+// }
static public IStream stream(final Object coll) throws Exception{
if(coll == null)
@@ -488,7 +486,9 @@ static public IStream stream(final Object coll) throws Exception{
}
static ISeq seqFrom(Object coll){
- if(coll instanceof Iterable)
+ if(coll instanceof Seqable)
+ return ((Seqable) coll).seq();
+ else if(coll instanceof Iterable)
return IteratorSeq.create(((Iterable) coll).iterator());
else if(coll.getClass().isArray())
return ArraySeq.createFromObject(coll);
@@ -544,8 +544,8 @@ static public ISeq cons(Object x, Object coll){
//ISeq y = seq(coll);
if(coll == null)
return new PersistentList(x);
- else if (coll instanceof Sequence)
- return new Cons(x, (Sequence) coll);
+ else if (coll instanceof ISeq)
+ return new Cons(x, (ISeq) coll);
else
return new Cons(x, seq(coll));
}
@@ -580,7 +580,7 @@ static public ISeq next(Object x){
return seq.next();
}
-static public Sequence more(Object x){
+static public ISeq more(Object x){
if(x instanceof ISeq)
return ((ISeq) x).more();
ISeq seq = seq(x);
@@ -1254,7 +1254,7 @@ static public void print(Object x, Writer w) throws Exception{
}
if(x == null)
w.write("nil");
- else if(x instanceof Sequence || x instanceof IPersistentList)
+ else if(x instanceof ISeq || x instanceof IPersistentList)
{
w.write('(');
printInnerSeq(seq(x), w);
diff --git a/src/jvm/clojure/lang/Sequence.java b/src/jvm/clojure/lang/Sequence.java
deleted file mode 100644
index cb6b38eb..00000000
--- a/src/jvm/clojure/lang/Sequence.java
+++ /dev/null
@@ -1,16 +0,0 @@
-/**
- * Copyright (c) Rich Hickey. All rights reserved.
- * The use and distribution terms for this software are covered by the
- * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
- * which can be found in the file epl-v10.html at the root of this distribution.
- * By using this software in any fashion, you are agreeing to be bound by
- * the terms of this license.
- * You must not remove this notice, or any other, from this software.
- **/
-
-/* rich Jan 29, 2009 */
-
-package clojure.lang;
-
-public interface Sequence extends IPersistentCollection, Sequential{
-}