From 314053a9ebac78537f3b806c8c05f55f793b1954 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sun, 26 Nov 2017 11:04:53 -0500 Subject: [PATCH] Improve performance slightly --- fitnesse/FitNesseRoot/RecentChanges.wiki | 2 +- .../TranscendentalLisp/Recursion.wiki | 3 ++- lisp/util/list-builder.lisp | 7 +++++++ src/sexpression/Cons.java | 15 ++++++++------- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/fitnesse/FitNesseRoot/RecentChanges.wiki b/fitnesse/FitNesseRoot/RecentChanges.wiki index 56d131a..5b73d8f 100644 --- a/fitnesse/FitNesseRoot/RecentChanges.wiki +++ b/fitnesse/FitNesseRoot/RecentChanges.wiki @@ -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| diff --git a/fitnesse/FitNesseRoot/TranscendentalLisp/Recursion.wiki b/fitnesse/FitNesseRoot/TranscendentalLisp/Recursion.wiki index 363377e..52cc1e7 100644 --- a/fitnesse/FitNesseRoot/TranscendentalLisp/Recursion.wiki +++ b/fitnesse/FitNesseRoot/TranscendentalLisp/Recursion.wiki @@ -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) | =~/\)\)$/ | diff --git a/lisp/util/list-builder.lisp b/lisp/util/list-builder.lisp index b0501be..11b4151 100644 --- a/lisp/util/list-builder.lisp +++ b/lisp/util/list-builder.lisp @@ -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)))) diff --git a/src/sexpression/Cons.java b/src/sexpression/Cons.java index 52fbf50..4789224 100644 --- a/src/sexpression/Cons.java +++ b/src/sexpression/Cons.java @@ -39,18 +39,19 @@ public class Cons extends SExpression { @Override public String toString() { - return toStringTailRecursive("(").invoke(); + return toStringTailRecursive(new StringBuilder("(")).invoke(); } - private TailCall toStringTailRecursive(String precedingString) { - String leadingString = precedingString + first.toString(); + private TailCall 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()); } }