Improve performance slightly

This commit is contained in:
Mike Cifelli 2017-11-26 11:04:53 -05:00
parent 10ff762647
commit 314053a9eb
4 changed files with 18 additions and 9 deletions

View File

@ -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|

View File

@ -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) | =~/\)\)$/ |

View File

@ -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))))

View File

@ -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());
}
}