diff options
author | Rich Hickey <richhickey@gmail.com> | 2006-06-20 13:10:19 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2006-06-20 13:10:19 +0000 |
commit | 5d34c73c9fb03d82a65b916af246238aefb6bcb8 (patch) | |
tree | 4fa664ff8287153a1d3944c96fbc468cddaa97ae | |
parent | 093ea7b07aea48b9f4b8aac3a8beddfe2e1226f7 (diff) |
added caching of rest
-rw-r--r-- | src/cli/runtime/FnSeq.cs | 17 | ||||
-rw-r--r-- | src/jvm/clojure/lang/FnSeq.java | 13 |
2 files changed, 26 insertions, 4 deletions
diff --git a/src/cli/runtime/FnSeq.cs b/src/cli/runtime/FnSeq.cs index 6225cc05..e98a3635 100644 --- a/src/cli/runtime/FnSeq.cs +++ b/src/cli/runtime/FnSeq.cs @@ -17,19 +17,30 @@ public class FnSeq : ISeq{ Object _first;
IFn restFn;
+volatile ISeq _rest;
public FnSeq(Object first, IFn restFn) {
this._first = first;
this.restFn = restFn;
-}
+ this._rest = this;
+ }
public Object first() {
return _first;
}
public ISeq rest() {
- return (ISeq) restFn.invoke();
-}
+ if(_rest != this)
+ return _rest;
+ lock(this){
+ if(_rest == this)
+ {
+ _rest = (ISeq) restFn.invoke();
+ restFn = null;
+ }
+ return _rest;
+ }
+ }
}
}
diff --git a/src/jvm/clojure/lang/FnSeq.java b/src/jvm/clojure/lang/FnSeq.java index f20dc39c..6f59a437 100644 --- a/src/jvm/clojure/lang/FnSeq.java +++ b/src/jvm/clojure/lang/FnSeq.java @@ -14,10 +14,12 @@ public class FnSeq implements ISeq{ Object _first;
IFn restFn;
+volatile ISeq _rest;
public FnSeq(Object first, IFn restFn) {
this._first = first;
this.restFn = restFn;
+ this._rest = this;
}
public Object first() {
@@ -25,6 +27,15 @@ public Object first() { }
public ISeq rest() throws Exception {
- return (ISeq) restFn.invoke();
+ if(_rest != this)
+ return _rest;
+ synchronized(this){
+ if(_rest == this)
+ {
+ _rest = (ISeq) restFn.invoke();
+ restFn = null;
+ }
+ return _rest;
+ }
}
}
|