aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Allred <code@seanallred.com>2014-11-01 02:05:14 -0400
committerSean Allred <code@seanallred.com>2014-11-01 13:17:22 -0400
commit90dedf05061aa173143b27fea98de78e0c316dd4 (patch)
tree6e3b503a7ec5ef5a970a843b544248710fbde7bf
parent3dee01a971ce925ec2860e0e902ae85ce90d8ac7 (diff)
Add function to filter data structure
Only works on flat structures right now. This should be used and let-bound to a variable when that variable is going to be used a lot. Theoretically, it should be faster than traversing through the entire data structure. Think of this as a Emacs-local version of the API's own filter.
-rw-r--r--stack-core.el9
-rw-r--r--tests.el14
2 files changed, 23 insertions, 0 deletions
diff --git a/stack-core.el b/stack-core.el
index d48c873..cf7dde5 100644
--- a/stack-core.el
+++ b/stack-core.el
@@ -209,5 +209,14 @@ entire response as a complex alist."
stack-core-remaining-api-requests))
(cdr (assoc 'items response))))))))
+(defun stack-core-filter-data (data desired-tree)
+ "Filters DATA and returns the DESIRED-TREE"
+ (delq
+ nil
+ (mapcar (lambda (cons-cell)
+ (when (member (car cons-cell) desired-tree)
+ cons-cell))
+ data)))
+
(provide 'stack-core)
;;; stack-core.el ends here
diff --git a/tests.el b/tests.el
index c79cfc4..0c520d8 100644
--- a/tests.el
+++ b/tests.el
@@ -25,3 +25,17 @@
"Test a method given a bad set of keywords"
(should-error
(stack-core-make-request "questions" '(()))))
+
+(ert-deftest test-data-filter-1 ()
+ "Test the meta-convenience function"
+ (should
+ (equal
+ '((1 . t) (2 . [1 2]) (3))
+ (stack-core-filter-data '((0 . 3)
+ (1 . t)
+ (a . five)
+ (2 . [1 2])
+ ("5" . bop)
+ (3)
+ (p . 4))
+ '(1 2 3)))))