Improve performance slightly
This commit is contained in:
parent
10ff762647
commit
314053a9eb
|
@ -1,4 +1,4 @@
|
|||
|TranscendentalLisp.Recursion||10:47:57 Fri, Nov 24, 2017|
|
||||
|TranscendentalLisp.Recursion||10:09:13 Sun, Nov 26, 2017|
|
||||
|TranscendentalLisp||16:15:14 Fri, Mar 17, 2017|
|
||||
|TranscendentalLisp.Macros||10:10:15 Mon, Mar 13, 2017|
|
||||
|TranscendentalLisp.MacroTests||10:07:00 Mon, Mar 13, 2017|
|
||||
|
|
|
@ -25,4 +25,5 @@ Test recursion capabilities of various functions.
|
|||
| check | evaluate text | (apply 'progn big-list) | 1 |
|
||||
| check | evaluate text | (eval (append `(let ,(list-of '(x 20) 10000)) big-list)) | 1 |
|
||||
| check | evaluate text | (apply 'cond (list-of '((= 1 2) 1) 10000)) | NIL |
|
||||
| check | evaluate text | (eval (append '(case :a) (list-of '((:b :c :d) 1) 10000))) | NIL |
|
||||
| check | evaluate text | (eval (append '(case :a) (list-of '((:b :c :d) 1) 10000))) | NIL |
|
||||
| check | evaluate text | (nested-list 2000) | =~/\)\)$/ |
|
||||
|
|
|
@ -24,3 +24,10 @@
|
|||
(defun list-of-tail (seed item size)
|
||||
(if (< size 1) seed
|
||||
(recur (cons item seed) item (- size 1))))
|
||||
|
||||
(defun nested-list (size)
|
||||
(nested-list-tail () size))
|
||||
|
||||
(defun nested-list-tail (seed size)
|
||||
(if (< size 1) seed
|
||||
(recur (cons seed nil) (- size 1))))
|
||||
|
|
|
@ -39,18 +39,19 @@ public class Cons extends SExpression {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toStringTailRecursive("(").invoke();
|
||||
return toStringTailRecursive(new StringBuilder("(")).invoke();
|
||||
}
|
||||
|
||||
private TailCall<String> toStringTailRecursive(String precedingString) {
|
||||
String leadingString = precedingString + first.toString();
|
||||
private TailCall<String> toStringTailRecursive(StringBuilder leadingString) {
|
||||
leadingString.append(first.toString());
|
||||
|
||||
if (rest.isNull())
|
||||
return done(leadingString + ")");
|
||||
else if (rest.isCons())
|
||||
return tailCall(() -> ((Cons) rest).toStringTailRecursive(leadingString + " "));
|
||||
return done(leadingString.append(")").toString());
|
||||
else if (rest.isCons()) {
|
||||
return tailCall(() -> ((Cons) rest).toStringTailRecursive(leadingString.append(" ")));
|
||||
}
|
||||
|
||||
return done(leadingString + " . " + rest.toString() + ")");
|
||||
return done(leadingString.append(" . " + rest.toString() + ")").toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue