Implement dict-filter.
This commit is contained in:
parent
33f02bd329
commit
165794f317
1 changed files with 20 additions and 0 deletions
|
@ -32,6 +32,8 @@
|
||||||
dict-has-key? dict-ref
|
dict-has-key? dict-ref
|
||||||
dict-remove dict-set
|
dict-remove dict-set
|
||||||
dict-keys
|
dict-keys
|
||||||
|
dict-map
|
||||||
|
dict-filter
|
||||||
dictionary-tests!
|
dictionary-tests!
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -102,6 +104,19 @@
|
||||||
(cons k (if both? (proc k v) (proc v)))))
|
(cons k (if both? (proc k v) (proc v)))))
|
||||||
d)))
|
d)))
|
||||||
|
|
||||||
|
;; Returns a dictionary containing only kv pairs matching the
|
||||||
|
;; predicate which must accept two arguments. Unlike list filter,
|
||||||
|
;; does not perform final reverse on the result.
|
||||||
|
(define (dict-filter pred? d)
|
||||||
|
(let loop ((d d)
|
||||||
|
(r '()))
|
||||||
|
(if (null? d)
|
||||||
|
r
|
||||||
|
(loop (cdr d)
|
||||||
|
(if (pred? (caar d) (cdar d))
|
||||||
|
(cons (car d) r)
|
||||||
|
r)))))
|
||||||
|
|
||||||
;; Performs self-tests of the dictionary module.
|
;; Performs self-tests of the dictionary module.
|
||||||
(define (dictionary-tests!)
|
(define (dictionary-tests!)
|
||||||
(run-tests
|
(run-tests
|
||||||
|
@ -124,6 +139,11 @@
|
||||||
(b . 2)))
|
(b . 2)))
|
||||||
'((a . 2)
|
'((a . 2)
|
||||||
(b . 4)))
|
(b . 4)))
|
||||||
|
(test-equal? dict-filter (dict-filter (lambda (k v)
|
||||||
|
(odd? v))
|
||||||
|
'((a . 1)
|
||||||
|
(b . 2)))
|
||||||
|
'((a . 1)))
|
||||||
))
|
))
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue