[PATCH] Restructure notmuch-hello-reflect

Sebastian Spaeth Sebastian at SSpaeth.de
Fri Apr 30 05:28:02 PDT 2010


This function was "not lispy" according to a comment by dme, and it
also relied on a helper function that used 'cl code. Do away with all
that and provide a hopefully lispy solution that relies on elisp only
to "reflect" a (possibly non-square) matrix along it's diagonal.

Remove now unused notmuch-hello-reflect-generate-row function.

Remove unused notmuch-hello-roundup function (which did exactly the
same as (ceiling divident divisor) anyway).

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
 emacs/notmuch-hello.el |   36 ++++++++++++++----------------------
 1 files changed, 14 insertions(+), 22 deletions(-)

diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index 1358387..1999858 100644
--- a/emacs/notmuch-hello.el
+++ b/emacs/notmuch-hello.el
@@ -113,29 +113,21 @@
 	    maximize (length (car elem)))
       0))
 
-(defun notmuch-hello-roundup (dividend divisor)
-  "Return the rounded up value of dividing `dividend' by `divisor'."
-  (+ (/ dividend divisor)
-     (if (> (% dividend divisor) 0) 1 0)))
-
-(defun notmuch-hello-reflect-generate-row (ncols nrows row list)
-  (let ((len (length list)))
-    (loop for col from 0 to (- ncols 1)
-	  collect (let ((offset (+ (* nrows col) row)))
-		    (if (< offset len)
-			(nth offset list)
-		      ;; Don't forget to insert an empty slot in the
-		      ;; output matrix if there is no corresponding
-		      ;; value in the input matrix.
-		      nil)))))
-
 (defun notmuch-hello-reflect (list ncols)
-  "Reflect a `ncols' wide matrix represented by `list' along the
-diagonal."
-  ;; Not very lispy...
-  (let ((nrows (notmuch-hello-roundup (length list) ncols)))
-    (loop for row from 0 to (- nrows 1)
-	  append (notmuch-hello-reflect-generate-row ncols nrows row list))))
+  "Transpose a `ncols' wide matrix `list' along diagonal."
+  (let ((brows (ceiling (length list) ncols)) ;;#rows of new matrix
+        bvector offset)
+    (setq bvector (make-vector (length list) nil));;vector to be returned
+    (dotimes (pos (length list))
+      (aset bvector pos
+	    (elt list (setq offset 
+                            (cond ;;return row number if in first colum
+                             ((eq 0 (% pos ncols)) 
+                              (setq offset (floor pos ncols)))
+                             ((>= (% (length list) ncols) (% pos ncols)) 
+                              (+ offset brows)) ;;add brows for first 'long'cols
+                             (t (1- (+ offset brows)))))))) ;;+ brows-1 for remainder
+    (append bvector nil)));; return bvector as list
 
 (defun notmuch-hello-widget-search (widget &rest ignore)
   (notmuch-search (widget-get widget
-- 
1.7.0.4



More information about the notmuch mailing list