emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Achim Gratz <Stromeko@nexgo.de>
To: emacs-orgmode@gnu.org
Subject: [PATCH] do not use mapcar* for transposing tables
Date: Fri, 21 Jun 2013 22:48:05 +0200	[thread overview]
Message-ID: <87k3ln8a2i.fsf@Rainer.invalid> (raw)

[-- Attachment #1: Type: text/plain, Size: 315 bytes --]


I've recently stumbled over a mapcar* compiled into Org for transposing
tables.  This is a function from cl / cl-lib and should not be used at
runtime.  Since we can make some simplifying assumptions about the data
we'll have to deal with, re-implmenting the two uses with plain mapcar
was (relatively) painless.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-do-not-use-mapcar-for-transposing-tables.patch --]
[-- Type: text/x-patch, Size: 3209 bytes --]

From 8941eb1f7ed2a7f1164a92a957a53560127cc26f Mon Sep 17 00:00:00 2001
From: Achim Gratz <Stromeko@Stromeko.DE>
Date: Fri, 21 Jun 2013 22:37:51 +0200
Subject: [PATCH] do not use mapcar* for transposing tables

* lisp/ob-core.el (org-babel-get-rownames),
  lisp/org-table.el (org-table-transpose-table-at-point): Replace the
  inadvertent use of mapcar* (from cl) by plain mapcar and direct cons
  manipulation.

The error was not caught at compilation time since both source files
require cl during compilation for using cl macros.  These were the
only uses of mapcar* in Org, but I didn't check for other cl
_functions_ (as opposed to macros, which would need to be checked if
their implementation uses cl functions).
---
 lisp/ob-core.el   | 28 ++++++++++++----------------
 lisp/org-table.el | 15 ++++++++++-----
 2 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index e2cb03b..a22a696 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -1502,22 +1502,18 @@ (defun org-babel-get-colnames (table)
 (defun org-babel-get-rownames (table)
   "Return the row names of TABLE.
 Return a cons cell, the `car' of which contains the TABLE less
-colnames, and the `cdr' of which contains a list of the column
-names.  Note: this function removes any hlines in TABLE."
-  (let* ((trans (lambda (table) (apply #'mapcar* #'list table)))
-	 (width (apply 'max
-		       (mapcar (lambda (el) (if (listp el) (length el) 0)) table)))
-	 (table (funcall trans (mapcar (lambda (row)
-					 (if (not (equal row 'hline))
-					     row
-					   (setq row '())
-					   (dotimes (n width)
-					     (setq row (cons 'hline row)))
-					   row))
-				       table))))
-    (cons (mapcar (lambda (row) (if (equal (car row) 'hline) 'hline row))
-		  (funcall trans (cdr table)))
-	  (remove 'hline (car table)))))
+rownames, and the `cdr' of which contains a list of the rownames.
+Note: this function removes any hlines in TABLE."
+  (let* ((table (org-babel-del-hlines table))
+	 (rownames (funcall (lambda ()
+			      (let ((tp table))
+				(mapcar
+				 (lambda (row)
+				   (prog1
+				       (pop (car tp))
+				     (setq tp (cdr tp))))
+				 table))))))
+    (cons table rownames)))
 
 (defun org-babel-put-colnames (table colnames)
   "Add COLNAMES to TABLE if they exist."
diff --git a/lisp/org-table.el b/lisp/org-table.el
index 9468b2b..243449f 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -1870,11 +1870,16 @@ (defun org-table-transpose-table-at-point ()
 
 Note that horizontal lines disappeared."
   (interactive)
-  (let ((contents
-         (apply #'mapcar* #'list
-                ;; remove 'hline from list
-		(delq nil (mapcar (lambda (x) (when (listp x) x))
-				  (org-table-to-lisp))))))
+  (let* ((table (delete 'hline (org-table-to-lisp)))
+	 (contents (mapcar (lambda (p)
+			     (let ((tp table))
+			       (mapcar
+				(lambda (rown)
+				  (prog1
+				      (pop (car tp))
+				    (setq tp (cdr tp))))
+				table)))
+			   (car table))))
     (delete-region (org-table-begin) (org-table-end))
     (insert (mapconcat (lambda(x) (concat "| " (mapconcat 'identity x " | " ) "  |\n" ))
                        contents ""))
-- 
1.8.3


[-- Attachment #3: Type: text/plain, Size: 192 bytes --]



Regards,
Achim.
-- 
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

SD adaptation for Waldorf rackAttack V1.04R1:
http://Synth.Stromeko.net/Downloads.html#WaldorfSDada

             reply	other threads:[~2013-06-21 20:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-21 20:48 Achim Gratz [this message]
2013-06-27 14:22 ` [PATCH] do not use mapcar* for transposing tables Bastien
2013-06-27 18:53   ` Achim Gratz
2013-06-27 19:20     ` Bastien

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k3ln8a2i.fsf@Rainer.invalid \
    --to=stromeko@nexgo.de \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).