diff options
author | Sean Allred <code@seanallred.com> | 2014-11-01 18:58:34 -0400 |
---|---|---|
committer | Sean Allred <code@seanallred.com> | 2014-11-01 18:58:34 -0400 |
commit | 0943b14b1991b42ce838c5f2189af92bb62f6d96 (patch) | |
tree | 9faa7dff663b0d7501996e7eb99c6a9044dc8ebe /stack-core.el | |
parent | 9078fe4e6bb9d74e8fcd6020c1364526b41f35e5 (diff) |
Return the correct type from filter
Vectors should not be turning into lists. See test cases.
Diffstat (limited to 'stack-core.el')
-rw-r--r-- | stack-core.el | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/stack-core.el b/stack-core.el index ef0dc9f..37ae2af 100644 --- a/stack-core.el +++ b/stack-core.el @@ -212,21 +212,41 @@ entire response as a complex alist." (defun stack-core-filter-data (data desired-tree) "Filters DATA and returns the DESIRED-TREE" (if (vectorp data) - (mapcar (lambda (entry) - (stack-core-filter-data - entry desired-tree)) - data) + (apply #'vector + (mapcar (lambda (entry) + (stack-core-filter-data + entry desired-tree)) + data)) + (mapcar #'identity '(1 2 3)) (delq nil (mapcar (lambda (cons-cell) - (when (member (car cons-cell) desired-tree) - (if (and (sequencep (cdr cons-cell)) - (sequencep (elt (cdr cons-cell) 0))) - (stack-core-filter-data - (cdr cons-cell) - (cdr (assoc (car cons-cell) desired-tree)))) - cons-cell)) + (let ((f (stack-core-filter-data--item-in-tree + (car cons-cell) desired-tree))) + (when f + (if (and (sequencep (cdr cons-cell)) + (sequencep (elt (cdr cons-cell) 0))) + (cons (car cons-cell) + (stack-core-filter-data + (cdr cons-cell) (cdr f))) + cons-cell)))) data)))) +(defun stack-core-filter-data--item-in-tree (item tree) + "Check if ITEM is in the direct leaves of TREE + +For example, when called with (f 'item '(some item here)), the +return would be `t'. When called as (f 'item '(some (item here) +in a (deep structure))), `(item here)' would be returned. +" + (when tree + (if (equal item (car tree)) tree + (if (and (listp (car tree)) + (equal item (caar tree))) + (car tree) + (stack-core-filter-data--item-in-tree item (cdr tree)))))) + +(stack-core-filter-data--item-in-tree 'a '(b (a 1 b c) c)) + (provide 'stack-core) ;;; stack-core.el ends here |