Git Product home page Git Product logo

Comments (6)

kostafey avatar kostafey commented on September 17, 2024

@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?

from clomacs.

nivekuil avatar nivekuil commented on September 17, 2024

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.

from clomacs.

kostafey avatar kostafey commented on September 17, 2024

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

from clomacs.

nivekuil avatar nivekuil commented on September 17, 2024

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.

from clomacs.

kostafey avatar kostafey commented on September 17, 2024

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?

from clomacs.

nivekuil avatar nivekuil commented on September 17, 2024

Yup, thanks.

from clomacs.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.