unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* small seq function for json data
@ 2016-10-16  5:29 Stefan Huchler
  2016-10-16  7:25 ` Helmut Eller
  2016-10-17  8:34 ` Nicolas Petton
  0 siblings, 2 replies; 4+ messages in thread
From: Stefan Huchler @ 2016-10-16  5:29 UTC (permalink / raw)
  To: emacs-devel

Hello,

accessing data coming from json requests can be a little bit anoying
in elisp I think.

So inspired by let-alist I wrote a more powerful version that supports
also vector which is also used in json messages.

(setq x '((foo . [((bar . "string of interest"))])))

(defun sbit-seq-get (seq path)
  (cond ((null path) seq)
	 ((listp seq)
	  (sbit-seq-get (cdr (assoc (car path) seq)) (cdr path))
	  )
	((vectorp seq)
	 (sbit-seq-get (elt seq (car path)) (cdr path))
	 )
	(t seq)))

(sbit-seq-get x '(foo 0 bar))

alternative manuall access would look like that:
(cdr (assoc 'bar (elt (cdr (assoc 'foo x))0)))

or with alist-get:
(let-alist (elt (let-alist x .foo) 0 ) .bar)


Does that make sense? Maybe integrate something similar to seq.el?
Eventualy plist support could maybe added, too. I dont know :)

I have more of a python background there its also fine to mix such keys
like:

x["foo"][0]["bar"]

Also I find it pretty inconsistent that elt as example uses the key
after the sequence also let-alist, but assoc and nth has first the key
as parameter, key first makes more sense in my oppinion (but) maybe I
am to bad with paredit to jump to the end of the parens :)

Well shure it would be a pain in the ass to change all addons so
I guess we have to live with that :)

Any thoughts on that?




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

end of thread, other threads:[~2016-10-18  3:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-10-16  5:29 small seq function for json data Stefan Huchler
2016-10-16  7:25 ` Helmut Eller
2016-10-17  8:34 ` Nicolas Petton
2016-10-18  3:04   ` Stefan Huchler

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.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).