New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
idea: dash destructurings #20
Comments
I think this is a good idea, and have added an attempt to allow destructuring with Dash. I am not very familiar with Dash, so please try it out. I'm sure that I missed some things. To enable it, you can load the new package like this: (use-package loopy
:straight (loopy :type git :host github :repo "okamsn/loopy"
:files (:defaults (:exclude "loopy-dash.el"))))
;; Optional support for destructuring with Dash.
(use-package loopy-dash
:after (loopy)
:demand t
:straight (loopy-dash :type git :host github :repo "okamsn/loopy"
:files ("loopy-dash.el"))) To use it, use ;; => (((1 (2 3)) (4 (5 6))) ; whole
;; (1 4) ; i
;; (2 5) ; j
;; (3 6)) ; k
(loopy (flag dash)
(loop (list elem '((1 (2 3)) (4 (5 6))))
(collect (whole &as i (j k)) elem))) To use it all of the time, customize I will try to add |
That's neat! I'm particularly excited about being able to extend dash's destructuring and add features like this one. We should add the link to the relevant dash documentation as well as referencing it.
I think it is similar. I'm not a fan of it's syntax. EDIT: |
I've added destructuring with I just made the flags All of these destructuring methods have their own peculiarities. In particular, For example, (seq-let (a b (c d))
'(1 2 (3 4))
'(a b (c d)))
(pcase-let* ((`(,a ,b (,c ,d))
'(1 2 (3 4))))
`(,a ,b (,c ,d))) expands into ;; `seq-let':
(let* ((x1246 (seq--elt-safe '(1 2 (3 4)) 2))
(x1247 (seq--elt-safe x1246 1))
(x1248 (seq--elt-safe x1246 0))
(x1249 (seq--elt-safe '(1 2 (3 4)) 1))
(x1250 (seq--elt-safe '(1 2 (3 4)) 0)))
(let ((c x1248)
(d x1247)
(b x1249)
(a x1250))
'(a b (c d))))
;; `pcase':
(let* ((x1267 (car '(1 2 (3 4))))
(x1268 (cdr '(1 2 (3 4))))
(x1269 (car x1268))
(x1270 (cdr x1268))
(x1271 (car x1270))
(x1272 (car x1271))
(x1273 (cdr x1271))
(x1274 (car x1273))
(x1275 (cdr x1273))
(x1276 (cdr x1270)))
(let ((c x1272)
(d x1274)
(b x1269)
(a x1267))
`(,a ,b (,c ,d)))) but I'm not sure whether this behavior is reliable or likely to change. Example of (ert-deftest list-destructuring ()
(should (and (equal '(5 6)
(eval (quote (loopy (flag pcase)
((list `(,a . ,b)
'((1 . 2) (3 . 4) (5 . 6))))
(finally-return a b)))))
(equal '(5 (6))
(eval (quote (loopy (flag pcase)
((list `(,a . ,b)
'((1 2) (3 4) (5 6))))
(finally-return a b)))))
(equal '(4 5 6 7)
(eval (quote (loopy (flag pcase)
((list `(,a ,b ,c ,d)
'((1 2 3 4) (4 5 6 7))))
(finally-return a b c d)))))
(equal '(4 5 6)
(eval (quote (loopy (flag pcase)
((list `[,i ,j ,k] '([1 2 3] [4 5 6])))
(return i j k)))))))) Example of (ert-deftest list-destructuring ()
(should (and (equal '(5 6)
(eval (quote (loopy (flag seq)
((list (a &rest b)
'((1 . 2) (3 . 4) (5 . 6))))
(finally-return a b)))))
(equal '(5 (6))
(eval (quote (loopy (flag seq)
((list (a &rest b)
'((1 2) (3 4) (5 6))))
(finally-return a b)))))
(equal '(4 5 6 7)
(eval (quote (loopy (flag seq)
((list (a b c d)
'((1 2 3 4) (4 5 6 7))))
(finally-return a b c d)))))
(equal '(4 5 6)
(eval (quote (loopy (flag seq)
((list [i j k] '([1 2 3] [4 5 6])))
(return i j k)))))))) |
As I was using loopy it occurred to me that there are cases where I may want to use some of the features provided by
dash
's destructuring. Perhaps others using loopy might want to as well. Right nowloopy
anddash
(not to mentioncl-destructuring-bind
andpcase
) implement their own destructuring). What would be cool is if loopy could easily use destructuring provided by another package.As a side-issue--not sure if this should be the responsibility of loopy, perhaps this should be a package. There should be a
destructuring
package that implements a cutomizable destructuring and is designed for other packages to use if they need destructuring for some purpose (like loopy does). This would make it so that packages don't each implement their own destructuring; also, it would provide a uniform destructuring experience among several packages. By customizable I mean that it should be easy to add your own "unorthodox" destructuring idioms (like dash does for example).The text was updated successfully, but these errors were encountered: