[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 3f7bc26 132/316: Provided option to extend destruc
From: |
ELPA Syncer |
Subject: |
[elpa] externals/dash 3f7bc26 132/316: Provided option to extend destructoring |
Date: |
Mon, 15 Feb 2021 15:57:42 -0500 (EST) |
branch: externals/dash
commit 3f7bc26ec692dd7fed756ff1e710be5a61a70a67
Author: Ivan Yonchovski <yyoncho@gmail.com>
Commit: Ivan Yonchovski <yyoncho@gmail.com>
Provided option to extend destructoring
The descructoring could be extended by defining a function named
`dash-expand:&{destructoring-key}` where {destructoring-key} stands for the
key
that will be used in the let destructoring, e. g. &hash/&plist in the
standart
dash
---
dash.el | 36 +++++++++++++++++++++++++++---------
dev/examples.el | 15 +++++++++++++--
2 files changed, 40 insertions(+), 11 deletions(-)
diff --git a/dash.el b/dash.el
index 5822361..9bb9908 100644
--- a/dash.el
+++ b/dash.el
@@ -1650,6 +1650,10 @@ All returned symbols are guaranteed to be unique."
(t
(cons (list s source) (dash--match-cons-1 match-form s))))))
+(defun dash--get-expand-function (type)
+ "Get expand function name for TYPE."
+ (intern (format "dash-expand:%s" type)))
+
(defun dash--match-cons-1 (match-form source &optional props)
"Match MATCH-FORM against SOURCE.
@@ -1669,7 +1673,7 @@ SOURCE is a proper or improper list."
((cdr match-form)
(cond
((and (symbolp (car match-form))
- (memq (car match-form) '(&keys &plist &alist &hash)))
+ (functionp (dash--get-expand-function (car match-form))))
(dash--match-kv (dash--match-kv-normalize-match-form match-form)
(dash--match-cons-get-cdr skip-cdr source)))
((dash--match-ignore-place-p (car match-form))
(dash--match-cons-1 (cdr match-form) source
@@ -1810,6 +1814,25 @@ kv can be any key-value store, such as plist, alist or
hash-table."
(t
(cons (list s source) (dash--match-kv-1 (cdr match-form) s (car
match-form)))))))
+(defun dash-expand:&hash (key source)
+ "Generate extracting KEY from SOURCE for &hash destructuring."
+ `(gethash ,key ,source))
+
+(defun dash-expand:&plist (key source)
+ "Generate extracting KEY from SOURCE for &plist destructuring."
+ `(plist-get ,source ,key))
+
+(defun dash-expand:&alist (key source)
+ "Generate extracting KEY from SOURCE for &alist destructuring."
+ `(cdr (assoc ,key ,source)))
+
+(defun dash-expand:&hash? (key source)
+ "Generate extracting KEY from SOURCE for &hash? destructuring.
+Similar to &hash but check whether the map is not nil."
+ `(when ,source (gethash ,key ,source)))
+
+(defalias 'dash-expand:&keys 'dash-expand:&plist)
+
(defun dash--match-kv-1 (match-form source type)
"Match MATCH-FORM against SOURCE of type TYPE.
@@ -1827,13 +1850,8 @@ Valid values are &plist, &alist and &hash."
(lambda (kv)
(let* ((k (car kv))
(v (cadr kv))
- (getter (cond
- ((or (eq type '&plist) (eq type '&keys))
- `(plist-get ,source ,k))
- ((eq type '&alist)
- `(cdr (assoc ,k ,source)))
- ((eq type '&hash)
- `(gethash ,k ,source)))))
+ (getter
+ (funcall (dash--get-expand-function type) k
source)))
(cond
((symbolp v)
(list (list v getter)))
@@ -1866,7 +1884,7 @@ Key-value stores are disambiguated by placing a token
&plist,
(let ((s (car match-form)))
(cons (list s source)
(dash--match (cddr match-form) s))))
- ((memq (car match-form) '(&keys &plist &alist &hash))
+ ((functionp (dash--get-expand-function (car match-form)))
(dash--match-kv (dash--match-kv-normalize-match-form match-form) source))
(t (dash--match-cons match-form source))))
((vectorp match-form)
diff --git a/dev/examples.el b/dev/examples.el
index a250b34..2c95a37 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -28,6 +28,11 @@
(defun square (num) (* num num))
(defun three-letters () '("A" "B" "C"))
+(defun dash-expand:&hash-or-plist (key source)
+ "Sample destructoring which works with plists and hash-tables."
+ `(if (hash-table-p ,source) (gethash ,key ,source)
+ (plist-get ,source ,key)))
+
;; Allow approximate comparison of floating-point results, to work
;; around differences in implementation between systems. Use the `~>'
;; symbol instead of `=>' to test the expected and actual values with
@@ -1142,7 +1147,8 @@ new list."
(puthash :foo 1 hash)
(puthash :bar 2 hash)
(-let (((&hash :foo :bar) hash)) (list foo bar))) => '(1 2)
- (-let (((_ &keys :foo :bar) (list 'ignored :foo 1 :bar 2))) (list foo
bar)) => '(1 2)
+ (-let (((&hash :foo (&hash? :bar)) (make-hash-table)))) => nil
+ (-let (((_ &keys :foo :bar) (list 'ignored :foo 1 :bar 2))) (list foo
bar)) => '(1 2)
;;; go over all the variations of match-form derivation
(-let (((&plist :foo foo :bar) (list :foo 1 :bar 2))) (list foo bar)) =>
'(1 2)
(-let (((&plist :foo foo :bar bar) (list :foo 1 :bar 2))) (list foo bar))
=> '(1 2)
@@ -1181,7 +1187,12 @@ new list."
(-let [(list &as _ _ _ a _ _ _ b _ _ _ c) (list 1 2 3 4 5 6 7 8 9 10 11
12)] (list a b c list)) => '(4 8 12 (1 2 3 4 5 6 7 8 9 10 11 12))
(-let (((x &as a b) (list 1 2))
((y &as c d) (list 3 4)))
- (list a b c d x y)) => '(1 2 3 4 (1 2) (3 4)))
+ (list a b c d x y)) => '(1 2 3 4 (1 2) (3 4))
+ (-let (((&hash-or-plist :key) (--doto (make-hash-table)
+ (puthash :key "value" it))))
+ key) => "value"
+ (-let (((&hash-or-plist :key) '(:key "value")))
+ key) => "value")
(defexamples -let*
(-let* (((a . b) (cons 1 2))
- [elpa] externals/dash 55d8cc9 088/316: Running sum and product can only be computed from non-empty lists, (continued)
- [elpa] externals/dash 55d8cc9 088/316: Running sum and product can only be computed from non-empty lists, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 1b9061c 090/316: Update docs, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 528e5a5 095/316: Merge pull request #258 from Wilfred/quotes-in-docs, ELPA Syncer, 2021/02/15
- [elpa] externals/dash c77644b 092/316: Regenerate docs, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 27c2fe8 100/316: Add missing keywords, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 83190a2 106/316: Emacs 26.1 was released and pretest does no longer exist, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 0505f5d 109/316: Merge pull request #271 from magnars/feature/-setq, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 148404c 114/316: [Fix #256] Allow `-let` bindings with no explicit value, ELPA Syncer, 2021/02/15
- [elpa] externals/dash f1ffdf7 131/316: Merge pull request #288 from cireu/master, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 6d2decb 122/316: Prefer number-to-string over int-to-string, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 3f7bc26 132/316: Provided option to extend destructoring,
ELPA Syncer <=
- [elpa] externals/dash a6e1436 124/316: Add -common-suffix, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 016e0de 133/316: Generalize `-rotate` for `|n|` greater than `(length list)`., ELPA Syncer, 2021/02/15
- [elpa] externals/dash bbf8016 138/316: Remove dependecy `cl-lib` in tests., ELPA Syncer, 2021/02/15
- [elpa] externals/dash f3e9cd2 151/316: Fix doc and test harnesses (#309), ELPA Syncer, 2021/02/15
- [elpa] externals/dash 070b569 156/316: Add missing indent declaration for ‘-some->’ and siblings, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 800c25d 166/316: feat(docs): simplify (function x) to #'x, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 53323ec 169/316: Fix typo and its copies, ELPA Syncer, 2021/02/15
- [elpa] externals/dash fe9bbc2 170/316: Merge pull request #334 from tarsiiformes/typo, ELPA Syncer, 2021/02/15
- [elpa] externals/dash e4e5269 180/316: Finish last change to package keywords, ELPA Syncer, 2021/02/15
- [elpa] externals/dash b329938 182/316: Update CI configuration, ELPA Syncer, 2021/02/15