A couple of examples, followed by patch:<br><br>(ps (destructuring-bind (a . rest) list<br> (blah a rest)))<br>=><br>"var a = list[0];<br>var rest = list.length > 1 ? list.slice(1) : null;<br>blah(a, rest);"<br>
<br>(ps (destructuring-bind ((a (b c)) (d e)) list))<br>=><br>"var _js42 = list[0];<br>var a = _js42[0];<br>var _js43 = _js42[1];<br>var b = _js43[0];<br>var c = _js43[1];<br>var _js44 = list[1];<br>var d = _js44[0];<br>
var e = _js44[1];"<br><br><br>From 74d29fe528d40795071e61df4fb1e4b56b28f049 Mon Sep 17 00:00:00 2001<br>From: Daniel Gackle <<a href="mailto:danielgackle@gmail.com">danielgackle@gmail.com</a>><br>Date: Fri, 10 Jul 2009 16:31:55 -0600<br>
Subject: [PATCH 1/2] DESTRUCTURING-BIND can now handle dotted and nested binding lists.<br><br>---<br> src/lib/ps-macro-lib.lisp | 31 +++++++++++++++++++++----------<br> 1 files changed, 21 insertions(+), 10 deletions(-)<br>
<br>diff --git a/src/lib/ps-macro-lib.lisp b/src/lib/ps-macro-lib.lisp<br>index 1013939..d70561c 100644<br>--- a/src/lib/ps-macro-lib.lisp<br>+++ b/src/lib/ps-macro-lib.lisp<br>@@ -144,14 +144,25 @@<br> (first args))))<br>
`((@ ,fn :apply) this ,arglist)))<br> <br>-(defpsmacro destructuring-bind (vars expr &body body)<br>- ;; a simple implementation that for now only supports flat lists,<br>- ;; but does allow NIL bindings to indicate ignore (a la LOOP)<br>
+(defun destructuring-wrap (arr n bindings body)<br>+ (cond ((null bindings)<br>+ body)<br>+ ((atom bindings)<br>+ ;; dotted destructuring list<br>+ `(let ((,bindings (when (> (length ,arr) ,n)<br>
+ ((@ ,arr :slice) ,n))))<br>+ ,body))<br>+ (t (let ((var (car bindings))<br>+ (inner-body (destructuring-wrap arr (1+ n) (cdr bindings) body)))<br>+ (cond ((null var) inner-body)<br>
+ ((atom var) `(let ((,var (aref ,arr ,n)))<br>+ ,inner-body))<br>+ (t `(destructuring-bind ,var (aref ,arr ,n)<br>+ ,inner-body)))))))<br>
+<br>+(defpsmacro destructuring-bind (bindings expr &body body)<br> (let* ((arr (if (complex-js-expr? expr) (ps-gensym) expr))<br>- (n -1)<br>- (bindings<br>- (append (unless (equal arr expr) `((,arr ,expr)))<br>
- (mapcan (lambda (var)<br>- (incf n)<br>- (when var `((,var (aref ,arr ,n))))) vars))))<br>- `(let* ,bindings ,@body)))<br>+ (bound (destructuring-wrap arr 0 bindings (cons 'progn body))))<br>
+ (if (eq arr expr)<br>+ bound<br>+ `(let ((,arr ,expr)) ,bound))))<br>-- <br>1.6.1<br><br><br>