clojure-emacs / clomacs

Simplifies Emacs Lisp interaction with Clojure and vice versa.
200 stars 21 forks source link

Passing map to clojure via alist #20

Closed nivekuil closed 4 years ago

nivekuil commented 4 years ago

Is this alist handling behavior expected? I don't know if there are some limitations here but it doesn't seem to recognize all elisp alists.

(json-alist-p '((1 2))) => t
(clomacs-alist-p '((1 2))) => nil
(clomacs-alist-to-map '((1 2))) => "{1 '(2)}"

In particular I'm trying to pass a map from elisp to clojure, and I'm not sure how to specify that other than an alist.

kostafey commented 4 years ago

@nivekuil, as far as I understand Emacs alist looks like '((:a . 1) (:b . 2)), whereas plist looks like '(:a 1 :b 2). So, if you need to pass a map to Clojure, you can store your data in alist and convert it by this function call: (clomacs-alist-to-map '((:a . 1) (:b . 2))) => "{:a 1 :b 2}"

Would you like to have clomacs-plist-to-map function in clomacs too?

nivekuil commented 4 years ago

Per the emacs manual, an alist is supposed to be a list of cons cells. I presume a list is also a cons cell because (consp '(:a 1 :b 2)) is t, so I think the json-alist-p behavior is correct in identifying that as an alist.

if you need to pass a map to Clojure, you can store your data in alist and convert it by this function call:

This doesn't become a map in clojure for me:

(clomacs-defun cl-type type)
(cl-type '((:a . 1))) => "clojure.lang.PersistentArrayMap"
(cl-type (clomacs-alist-to-map '((:a . 1))) => "java.lang.String"

In general I am curious how to get the latter case working, i.e. how I can pass from elisp a string that represents a map, into clojure where it then gets treated as an actual map.

Would you like to have clomacs-plist-to-map function in clomacs too?

I think that would be handy, as plists are the closer abstraction (non-duplicate keys). Could we just use json-plist-p for this? I think the actual conversion part is just a single character change from clomacs-alist-to-map, changing (cdr pair) to (cadr pair) on line 248.

kostafey commented 4 years ago

Am I missed something? https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html https://www.gnu.org/software/emacs/manual/html_node/elisp/Property-Lists.html#Property-Lists

What are you trying to do? Using alist in the Elisp side can always be considered as a map in Clojure side, whereas plist can be used as a list or a map in Elisp side, but it's a simple list in Clojure side. To unify that I assume users will use alist for maps to ensure they mean maps. E.g. you have a Clojure assoc function. Its parameters list are: (assoc map key val). So, you want to pass a map as it's the first argument. Ok, it will be auto-converted:

(clomacs-defun cl-assoc assoc)
(cl-assoc '((:a . 1) (:b . 2)) :c 3) => "{:a 1, :b 2, :c 3}"

So, normally, you should not call clomacs-alist-to-map manually. By default, Elisp side assumes output form Clojure as a string, but you can provide you conversion function in :return-type parameter of the clomacs-defun maco or use clomacs/format-result in Clojure side and :eval for autoconversion. E.g.: Clojure:

(require '(clomacs))

(defn my-assoc [map key val]
  (clomacs/format-result
   (assoc map key val)))

Elisp:

(clomacs-defun cl-my-assoc my-assoc
               :return-type :eval)
(cl-my-assoc '((:a . 1) (:b . 2)) :c 3) => ((:a . 1) (:b . 2) (:c . 3))
nivekuil commented 4 years ago

Using alist in the Elisp side can always be considered as a map in Clojure side, whereas plist can be used as a list or a map in Elisp side, but it's a simple list in Clojure side. To unify that I assume users will use alist for maps to ensure they mean maps.

That's a good point. parseedn-print-str seems to assume that any list with an even # of elements and keywords as keys is a map, and I expected clomacs to behave the same, but maybe the needs are different.

By default, Elisp side assumes output form Clojure as a string, but you can provide you conversion function in :return-type parameter of the clomacs-defun maco or use clomacs/format-result in Clojure side and :eval for autoconversion.

Ah, I missed this. Thanks.

kostafey commented 4 years ago

Unfortunately, clomacs documentation is a bit outdated. For more real examples please look at https://github.com/kostafey/ejc-sql/blob/master/ejc-interaction.el. Can I consider the issue closed?

nivekuil commented 4 years ago

Yup, thanks.