diff options
author | Rich Hickey <richhickey@gmail.com> | 2007-09-19 20:52:31 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2007-09-19 20:52:31 +0000 |
commit | 23554c4cd66e4821e39b21f60f8101868ac51070 (patch) | |
tree | ce4196a4483139a2729c03b49e6bbd5a695bc1b7 | |
parent | db86652ad2da15f500ad84806c2bf7a7af807741 (diff) |
refactoring
-rw-r--r-- | clojure.markdown | 34 | ||||
-rw-r--r-- | src/boot.clj | 10 | ||||
-rw-r--r-- | src/jvm/clojure/lang/ASeq.java | 14 | ||||
-rw-r--r-- | src/jvm/clojure/lang/IPersistentList.java | 6 | ||||
-rw-r--r-- | src/jvm/clojure/lang/IPersistentVector.java | 2 | ||||
-rw-r--r-- | src/jvm/clojure/lang/ISeq.java | 8 | ||||
-rw-r--r-- | src/jvm/clojure/lang/IStack.java | 19 | ||||
-rw-r--r-- | src/jvm/clojure/lang/LispReader.java | 3 | ||||
-rw-r--r-- | src/jvm/clojure/lang/PersistentList.java | 6 | ||||
-rw-r--r-- | src/jvm/clojure/lang/RT.java | 4 |
10 files changed, 73 insertions, 33 deletions
diff --git a/clojure.markdown b/clojure.markdown index f356be56..2d77b20d 100644 --- a/clojure.markdown +++ b/clojure.markdown @@ -217,7 +217,7 @@ A Symbol is *resolved*: If a Symbol has metadata, it may be used by the compiler, but will not be part of the resulting value. -Vectors and Maps yield vectors and maps whose contents are the *evaluated values* of the objects they contain. The same is true of metadata maps. If the vector or map has metadata, the *evaluated* metadata map will become the metadata of the resulting value. +Vectors and Maps yield vectors and (hash) maps whose contents are the *evaluated values* of the objects they contain. The same is true of metadata maps. If the vector or map has metadata, the *evaluated* metadata map will become the metadata of the resulting value. <pre><code> (def x 1) @@ -400,10 +400,10 @@ Clojure has a rich set of data structures. They share a set of properties: * Support metadata * Implement Iterable -### nil -Not properly a data structure, `nil` is a valid value of any data type in Clojure (since primitives are always boxed). `nil` has the same value as Java `null`. The Clojure conditional system is based around `nil`/non-nil, with `nil` representing the value of logical false in conditional tests. In addition, `nil` is used as the end-of-sequence sentinel value in the sequence protocol. +### _nil_ +`nil` is a possible value of any data type in Clojure (since primitives are always boxed). `nil` has the same value as Java `null`. The Clojure conditional system is based around `nil`/non-nil, with `nil` representing the value of logical false in conditional tests. In addition, `nil` is used as the end-of-sequence sentinel value in the sequence protocol. -### Nums +### _Nums_ All Clojure numbers are derived from clojure.lang.Num, which in turn is derived from java.lang.Number. There are 4 types: * FixNum @@ -424,13 +424,13 @@ All Clojure numbers are derived from clojure.lang.Num, which in turn is derived Any numeric operation involving DoubleNums yields a DoubleNum. -### Strings +### _Strings_ Clojure strings are Java `Strings`. -### Characters +### _Characters_ Clojure characters are Java `Characters`. -### Keywords +### _Keywords_ Keywords are symbolic identifiers that evaluate to themselves. They provide very fast equality tests. Like Symbols, they have names and optional namespaces, both of which are strings. The leading ':' is not part of the namespace or name. Keywords implement IFn, for invoke() of one argument, which they expect to be a map, in which they look themselves up, i.e. keywords are functions of maps. <pre><code> @@ -440,7 +440,7 @@ Keywords are symbolic identifiers that evaluate to themselves. They provide very > 2 </code></pre> -### Symbols +### _Symbols_ Symbols are identifiers that are normally used to refer to something else. They can be used in program forms to refer to function parameters, let bindings, class names and global vars. They have names and optional namespaces, both of which are strings. Symbols can have metadata. ### _Collections_ @@ -453,7 +453,7 @@ Returns the number of items in the collection. `(count nil)` returns `0`. --- ### (*conj* coll item) -Conj[oin]. Returns a new collection with the item 'added'. `(conj nil item)` returns `(item)`. +Conj[oin]. Returns a *new* collection with the item 'added'. `(conj nil item)` returns `(item)`. --- ### (*seq* coll) @@ -477,9 +477,18 @@ Returns a new list without the first item. If the list is empty, throws an excep ### _Maps_ (IPersistentMap) -Map keys to values. Two different map types are provided - hashed and sorted. Hash maps require keys that correctly support hashCode and equals. Sorted maps require keys that implement Comparable, or an instance of Comparator. Hash maps provide faster access O(log32N) vs O(logN), but sorted maps are, well, sorted. `count` is O(1). `conj` expects another (possibly single entry) map as the item, and returns a new map which is the old map plus the entries from the new, which may overwrite entries of the old. `seq` returns a sequence of map entries, which are key/value pairs. Maps implement IFn, for invoke() of one argument, which they presume is a key and look up in themselves, i.e. maps are functions of their keys. +Map keys to values. Two different map types are provided - hashed and sorted. Hash maps require keys that correctly support hashCode and equals. Sorted maps require keys that implement Comparable, or an instance of Comparator. Hash maps provide faster access O(log32N) vs O(logN), but sorted maps are, well, sorted. `count` is O(1). `conj` expects another (possibly single entry) map as the item, and returns a new map which is the old map plus the entries from the new, which may overwrite entries of the old. `seq` returns a sequence of map entries, which are key/value pairs. Sorted map also supports `rseq`, which returns the entries in reverse order. Maps implement IFn, for invoke() of one argument, which they presume is a key and look up in themselves, i.e. maps are functions of their keys. -They share these functions: +Map functions: + +--- +### (*hash-map* & keyvals*) +### (*sorted-map* & keyvals*) +### (*sorted-map-by* comparator & keyvals*) + +keyval => key val + +Returns a new hash/sorted map with supplied mappings. --- ### (*assoc* map key val) @@ -509,7 +518,8 @@ Returns a sequence of the map's keys. ### (*vals* map) Returns a sequence of the map's values. -### Vectors (IPersistentVector) +### _Vectors (IPersistentVector)_ + <h2 id="metadata">Metadata</h2> diff --git a/src/boot.clj b/src/boot.clj index f687c487..3e82cea8 100644 --- a/src/boot.clj +++ b/src/boot.clj @@ -202,6 +202,13 @@ (defn count [coll] (. RT (count coll))) +;;list stuff +(defn peek [list] + (. RT (peek list))) + +(defn pop [list] + (. RT (pop list))) + ;;map stuff (defn contains [coll key] @@ -225,6 +232,9 @@ (defn vals [map] (. RT (vals map))) +(defn rseq [smap] + (. smap (rseq))) + (defn andfn [& args] (if (nil? (rest args)) (first args) diff --git a/src/jvm/clojure/lang/ASeq.java b/src/jvm/clojure/lang/ASeq.java index 0a31797c..59206f1b 100644 --- a/src/jvm/clojure/lang/ASeq.java +++ b/src/jvm/clojure/lang/ASeq.java @@ -51,13 +51,13 @@ public int hashCode(){ return _hash;
}
-public Object peek(){
- return first();
-}
-
-public IPersistentList pop(){
- return rest();
-}
+//public Object peek(){
+// return first();
+//}
+//
+//public IPersistentList pop(){
+// return rest();
+//}
public int count(){
return 1 + RT.count(rest());
diff --git a/src/jvm/clojure/lang/IPersistentList.java b/src/jvm/clojure/lang/IPersistentList.java index 5a54485e..4fdf3ff9 100644 --- a/src/jvm/clojure/lang/IPersistentList.java +++ b/src/jvm/clojure/lang/IPersistentList.java @@ -11,10 +11,6 @@ package clojure.lang;
-public interface IPersistentList extends IPersistentCollection, Sequential {
-
- Object peek();
-
- IPersistentList pop();
+public interface IPersistentList extends Sequential, IStack{
}
diff --git a/src/jvm/clojure/lang/IPersistentVector.java b/src/jvm/clojure/lang/IPersistentVector.java index 971fb318..6f17f7e0 100644 --- a/src/jvm/clojure/lang/IPersistentVector.java +++ b/src/jvm/clojure/lang/IPersistentVector.java @@ -10,7 +10,7 @@ package clojure.lang; * You must not remove this notice, or any other, from this software.
*/
-public interface IPersistentVector extends Associative, Sequential{
+public interface IPersistentVector extends Associative, Sequential, IStack{
int length();
Object nth(int i);
diff --git a/src/jvm/clojure/lang/ISeq.java b/src/jvm/clojure/lang/ISeq.java index 52f0d5d2..08c66cf9 100644 --- a/src/jvm/clojure/lang/ISeq.java +++ b/src/jvm/clojure/lang/ISeq.java @@ -12,15 +12,15 @@ package clojure.lang; /**
* A persistent, functional, sequence interface
- *
+ * <p/>
* ISeqs are immutable values, i.e. neither first(), nor rest() changes
* or invalidates the ISeq
*/
-public interface ISeq extends IPersistentList{
+public interface ISeq extends IPersistentCollection{
-Object first() ;
+Object first();
-ISeq rest() ;
+ISeq rest();
ISeq cons(Object o);
diff --git a/src/jvm/clojure/lang/IStack.java b/src/jvm/clojure/lang/IStack.java new file mode 100644 index 00000000..c28b6796 --- /dev/null +++ b/src/jvm/clojure/lang/IStack.java @@ -0,0 +1,19 @@ +/** + * Copyright (c) Rich Hickey. All rights reserved. + * The use and distribution terms for this software are covered by the + * Common Public License 1.0 (http://opensource.org/licenses/cpl.php) + * which can be found in the file CPL.TXT 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 Sep 19, 2007 */ + +package clojure.lang; + +public interface IStack extends IPersistentCollection{ +Object peek(); + +IStack pop(); +} diff --git a/src/jvm/clojure/lang/LispReader.java b/src/jvm/clojure/lang/LispReader.java index 578204c0..94789d00 100644 --- a/src/jvm/clojure/lang/LispReader.java +++ b/src/jvm/clojure/lang/LispReader.java @@ -493,7 +493,8 @@ static class ListReader extends AFn{ List list = readDelimitedList(')', r, true);
if(list.isEmpty())
return PersistentList.EMPTY;
- IObj s = (IObj) RT.seq(list);
+ IObj s = (IObj) PersistentList.create(list);
+// IObj s = (IObj) RT.seq(list);
if(line != -1)
return s.withMeta(RT.map(LINE_KEY, line));
else
diff --git a/src/jvm/clojure/lang/PersistentList.java b/src/jvm/clojure/lang/PersistentList.java index f1130195..1b92ff1d 100644 --- a/src/jvm/clojure/lang/PersistentList.java +++ b/src/jvm/clojure/lang/PersistentList.java @@ -13,7 +13,7 @@ package clojure.lang; import java.util.List;
import java.util.ListIterator;
-public class PersistentList extends ASeq{
+public class PersistentList extends ASeq implements IPersistentList{
private final Object _first;
private final PersistentList _rest;
@@ -52,6 +52,10 @@ public ISeq rest(){ return _rest;
}
+public Object peek(){
+ return first();
+}
+
public IPersistentList pop(){
if(_rest == null)
return EMPTY.withMeta(meta());
diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java index 652c8337..8b432bc3 100644 --- a/src/jvm/clojure/lang/RT.java +++ b/src/jvm/clojure/lang/RT.java @@ -265,13 +265,13 @@ static public ISeq rrest(Object x){ static public Object peek(Object x){ if(x == null) return null; - return ((IPersistentList) x).peek(); + return ((IStack) x).peek(); } static public Object pop(Object x){ if(x == null) return null; - return ((IPersistentList) x).pop(); + return ((IStack) x).pop(); } static public Object get(Object coll, Object key){ |