emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Nicolas Goaziou <n.goaziou@gmail.com>
To: "András Major" <andras.g.major@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: Bug: Babel: asymptote: erroneous conversion	of heterogeneous-typed table
Date: Mon, 29 Aug 2011 18:35:02 +0200	[thread overview]
Message-ID: <87fwkkjhvt.fsf@gmail.com> (raw)
In-Reply-To: <87mxesjs9e.fsf@gmail.com> (Nicolas Goaziou's message of "Mon, 29 Aug 2011 14:50:53 +0200")

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

Completing myself.

> Though, you insist on being able to enter it as a number anyway,
> hoping ob-asymptote will do the magic behind. How could it, since the
> language can't itself?

Actually, the attached patch does that magic: if there's any string in
the table, every cell will be turned into a string and the array will be
of type string.

I still think a more general way would be better. I'll let Eric Schulte
decide what to do about it.

Regards,

-- 
Nicolas Goaziou

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-asymptote-mixed-types-tables-default-to-string-ar.patch --]
[-- Type: text/x-patch, Size: 4165 bytes --]

From 72dfd3c98c535ea595be053fe321e195cd08c070 Mon Sep 17 00:00:00 2001
From: Nicolas Goaziou <n.goaziou@gmail.com>
Date: Mon, 29 Aug 2011 17:24:07 +0200
Subject: [PATCH] ob-asymptote: mixed-types tables default to string arrays

* lisp/ob-asymptote.el (org-babel-asymptote-table-to-array): Require a
new argument TYPE specifying the detected type of array.  If it's a
string array, make sure every element is returned as a string. Also
improve doc-string.
(org-babel-asymptote-var-to-asymptote): Fill new argument.  Small
refactoring.
(org-babel-asymptote-define-type): Rewrite to avoid stopping search at
first float found, as strings have precedence over floats.
---
 lisp/ob-asymptote.el |   50 ++++++++++++++++++++++++++++----------------------
 1 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/lisp/ob-asymptote.el b/lisp/ob-asymptote.el
index 1cf6db4..0e9b449 100644
--- a/lisp/ob-asymptote.el
+++ b/lisp/ob-asymptote.el
@@ -108,23 +108,29 @@ a variable of the same value."
      ((stringp val)
       (format "string %S=\"%s\";" var val))
      ((listp val)
-      (let* ((dimension-2-p (not (null (cdr val))))
+      (let* ((dimension-2-p (cdr val))
              (dim (if dimension-2-p "[][]" "[]"))
              (type (org-babel-asymptote-define-type val))
              (array (org-babel-asymptote-table-to-array
-                     val
+                     val type
                      (if dimension-2-p '(:lstart "{" :lend "}," :llend "}")))))
         (format "%S%s %S=%s;" type dim var array))))))
 
-(defun org-babel-asymptote-table-to-array (table params)
-  "Convert values of an elisp table into a string of an asymptote array.
+(defun org-babel-asymptote-table-to-array (table type params)
+  "Convert values of TABLE into a string of an asymptote array.
+
+TABLE is a list whose atoms are assumed to be of type
+TYPE. PARAMS is a plist of parameters that can influence the
+conversion.
+
 Empty cells are ignored."
   (labels ((atom-to-string (table)
                            (cond
                             ((null table) '())
                             ((not (listp (car table)))
-                             (cons (if (and (stringp (car table))
-                                            (not (string= (car table) "")))
+                             (cons (if (or (eq type 'string)
+                                           (and (stringp (car table))
+                                                (not (string= (car table) ""))))
                                        (format "\"%s\"" (car table))
                                      (format "%s" (car table)))
                                    (atom-to-string (cdr table))))
@@ -140,22 +146,22 @@ Empty cells are ignored."
 
 (defun org-babel-asymptote-define-type (data)
   "Determine type of DATA.
-DATA is a list. Type symbol is returned as 'symbol. The type is
-usually the type of the first atom encountered, except for arrays
-of int, where every cell must be of int type."
-  (labels ((anything-but-int (el)
-                             (cond
-                              ((null el) nil)
-                              ((not (listp (car el)))
-                               (cond
-                                ((floatp (car el)) 'real)
-                                ((stringp (car el)) 'string)
-                                (t
-                                 (anything-but-int (cdr el)))))
-                              (t
-                               (or (anything-but-int (car el))
-                                   (anything-but-int (cdr el)))))))
-    (or (anything-but-int data) 'int)))
+
+DATA is a list.  Return type as a symbol.
+
+The type is `string' if any element in DATA is
+a string. Otherwise, it is either `float', if some elements are
+floats, or `int'."
+  (let* ((type 'int)
+	 (find-type
+	  (lambda (row)
+	    (catch 'exit
+	      (mapc (lambda (el)
+		      (cond ((listp el) (funcall find-type el))
+			    ((stringp el) (throw 'exit (setq type 'string)))
+			    ((floatp el) (setq type 'float))))
+		    row)))))
+    (funcall find-type data) type))
 
 (provide 'ob-asymptote)
 
-- 
1.7.6.1


  reply	other threads:[~2011-08-29 16:35 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-29  8:00 Bug: Babel: asymptote: erroneous conversion of heterogeneous-typed table Major A
2011-08-29  8:47 ` Nicolas Goaziou
2011-08-29  9:12   ` András Major
2011-08-29  9:50     ` Nicolas Goaziou
2011-08-29 10:42       ` András Major
2011-08-29 12:50         ` Nicolas Goaziou
2011-08-29 16:35           ` Nicolas Goaziou [this message]
2011-08-29 16:52             ` Eric Schulte
2011-08-29 17:27               ` Nicolas Goaziou
2011-08-29 18:02                 ` Eric Schulte
2011-08-29 18:50                   ` András Major
2011-08-29 19:02                     ` Eric Schulte
2011-08-29 19:11                       ` Nick Dokos
2011-08-30 19:12                       ` András Major
2011-08-30 19:34                         ` Eric Schulte
2011-08-30 20:45                           ` András Major
2011-08-30 20:55                             ` Eric Schulte
2011-08-31  6:48                               ` András Major
2011-08-31  8:17                                 ` Nick Dokos
2011-08-31  9:35                                   ` Nicolas Goaziou
2011-08-31 12:14                                     ` Eric Schulte
2011-08-29  9:05 ` Nick Dokos
2011-08-29  9:40   ` András Major

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=87fwkkjhvt.fsf@gmail.com \
    --to=n.goaziou@gmail.com \
    --cc=andras.g.major@gmail.com \
    --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).