From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: Bug: Babel: asymptote: erroneous conversion of heterogeneous-typed table Date: Mon, 29 Aug 2011 18:35:02 +0200 Message-ID: <87fwkkjhvt.fsf@gmail.com> References: <20110829080003.GA12790@discus> <87hb50li4b.fsf@gmail.com> <878vqclf64.fsf@gmail.com> <87mxesjs9e.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([140.186.70.92]:42297) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qy4oK-0004wW-Lo for emacs-orgmode@gnu.org; Mon, 29 Aug 2011 12:35:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Qy4oJ-0005mr-H0 for emacs-orgmode@gnu.org; Mon, 29 Aug 2011 12:35:44 -0400 Received: from mail-wy0-f169.google.com ([74.125.82.169]:51837) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qy4oJ-0005mn-9H for emacs-orgmode@gnu.org; Mon, 29 Aug 2011 12:35:43 -0400 Received: by wyi11 with SMTP id 11so4685991wyi.0 for ; Mon, 29 Aug 2011 09:35:42 -0700 (PDT) In-Reply-To: <87mxesjs9e.fsf@gmail.com> (Nicolas Goaziou's message of "Mon, 29 Aug 2011 14:50:53 +0200") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: =?utf-8?Q?Andr=C3=A1s?= Major Cc: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain 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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-ob-asymptote-mixed-types-tables-default-to-string-ar.patch >From 72dfd3c98c535ea595be053fe321e195cd08c070 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou 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 --=-=-=--