emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Send table rows to OCaml as tuples
@ 2022-02-23 19:39 necaris
  2022-11-17 11:35 ` Ihor Radchenko
  0 siblings, 1 reply; 2+ messages in thread
From: necaris @ 2022-02-23 19:39 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: Rami Chowdhury

From: Rami Chowdhury <necaris@gmail.com>

Previously, tables (i.e. lists-of-lists) came through as
arrays-of-arrays. Since OCaml arrays are required to be homogeneous in
their types, this meant tables with heterogenous types within a row
could not be handled. This change represents tables as arrays of tuples,
which is more flexible, while preserving the previous behavior for
lists.

* lisp/ob-ocaml.el (org-babel-ocaml-elisp-to-ocaml-tuple,
org-babel-ocaml-elisp-to-ocaml-array, org-babel-elisp-to-ocaml): Send
table rows as tuples
---
 lisp/ob-ocaml.el | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/lisp/ob-ocaml.el b/lisp/ob-ocaml.el
index 80df795..ad7df97 100644
--- a/lisp/ob-ocaml.el
+++ b/lisp/ob-ocaml.el
@@ -123,11 +123,23 @@
 			  (org-babel-ocaml-elisp-to-ocaml (cdr pair))))
    (org-babel--get-vars params)))
 
+(defun org-babel-ocaml-elisp-to-ocaml-tuple (val &optional nested-renderer-f)
+  "Return OCaml evaluating to VAL, with elements rendered by NESTED-RENDERER-F."
+  (let ((renderer (or nested-renderer-f #'org-babel-ocaml-elisp-to-ocaml)))
+    (concat "(" (mapconcat renderer val ", ") ")")))
+
+(defun org-babel-ocaml-elisp-to-ocaml-array (val &optional nested-renderer-f)
+  "Return OCaml evaluating to VAL, with elements rendered by NESTED-RENDERER-F."
+  (let ((renderer (or nested-renderer-f #'org-babel-ocaml-elisp-to-ocaml)))
+    (concat "[|" (mapconcat renderer val "; ") "|]")))
+
 (defun org-babel-ocaml-elisp-to-ocaml (val)
   "Return a string of ocaml code which evaluates to VAL."
-  (if (listp val)
-      (concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml val "; ") "|]")
-    (format "%S" val)))
+  (cond ((and (listp val) (listp (car val)))
+           (org-babel-ocaml-elisp-to-ocaml-array val #'org-babel-ocaml-elisp-to-ocaml-tuple))
+         ((listp val)
+           (org-babel-ocaml-elisp-to-ocaml-array val))
+        (t (format "%S" val))))
 
 (defun org-babel-ocaml-parse-output (value type)
   "Parse VALUE of type TYPE.
-- 
2.35.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-11-17 11:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-23 19:39 [PATCH] Send table rows to OCaml as tuples necaris
2022-11-17 11:35 ` Ihor Radchenko

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).