From: necaris@gmail.com
To: emacs-orgmode@gnu.org
Cc: Rami Chowdhury <necaris@gmail.com>
Subject: [PATCH] Send table rows to OCaml as tuples
Date: Wed, 23 Feb 2022 14:39:34 -0500 [thread overview]
Message-ID: <20220223193934.226994-1-necaris@gmail.com> (raw)
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
next reply other threads:[~2022-02-23 19:41 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-23 19:39 necaris [this message]
2022-11-17 11:35 ` [PATCH] Send table rows to OCaml as tuples Ihor Radchenko
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=20220223193934.226994-1-necaris@gmail.com \
--to=necaris@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).