git » jacl.git » commit 97a2fcd

APPLY/unspread

author Alan Dipert
2020-09-01 12:48:19 UTC
committer Alan Dipert
2020-09-01 12:48:19 UTC
parent bd64a19dacabd01348f2c6ff221702612bfaf68e

APPLY/unspread

boot.lisp +2 -2
jacl.js +8 -0
todo.org +3 -1

diff --git a/boot.lisp b/boot.lisp
index ad2bc9b..178033d 100644
--- a/boot.lisp
+++ b/boot.lisp
@@ -382,8 +382,8 @@
 
 (%export 'apply)
 (defun apply (function &rest args)
-  (\. function
-      (|apply| function (%js "Array.from(~{})" (%unspread args)))))
+  (declare (rest-array args))
+  (\. function (|apply| function (%js "unspread(~{})" args))))
 
 (%export 'do)
 (defmacro do (vars end &body body)
diff --git a/jacl.js b/jacl.js
index 77748a3..01123d9 100644
--- a/jacl.js
+++ b/jacl.js
@@ -2440,3 +2440,11 @@ const prstr = obj => {
     return obj.toString();
   }
 }
+
+const unspread = (arr) => {
+  if (arr.length && arr[arr.length-1] instanceof Cons) {
+    const more = arr.pop();
+    Array.prototype.push.apply(arr, Array.from(more));
+  }
+  return arr;
+}
diff --git a/todo.org b/todo.org
index 3de303c..8661449 100644
--- a/todo.org
+++ b/todo.org
@@ -21,7 +21,7 @@
 ** Current function name as part of dynamic environment
 * TODO FLET
 ** Use multiple values to return new env from macros like FLET?
-** TODO FUNCTION
+** DONE FUNCTION
 ** TODO MAPCAR
 ** TODO MACROLET
 * TODO File compilation
@@ -39,3 +39,5 @@ See mv.lisp. Make it possible for macros to return multiple values; then they ca
 * TODO Tree shaking
 * TODO JACL:DELIVER
 * TODO Host REPL
+* APPLY and FUNCALL
+These are relatively inefficient. APPLY could use %unspread-array with (declare rest-array)