File tree Expand file tree Collapse file tree 4 files changed +21
-4
lines changed
Expand file tree Collapse file tree 4 files changed +21
-4
lines changed Original file line number Diff line number Diff line change 1+ # 210_11
2+
3+ ## 2026/01/30 为hash-table补上hash-table-fold函数
Original file line number Diff line number Diff line change 2525 hash-table-set! hash-table-delete! hash-table-intern! hash-table-update!
2626 hash-table-update!/default hash-table-pop! hash-table-clear!
2727 hash-table-size hash-table-keys hash-table-values hash-table-entries
28- hash-table-find hash-table-count
28+ hash-table-find hash-table-count hash-table-fold
2929 hash-table-for-each hash-table-map->list
3030 hash-table->alist
3131 )
3232 (begin
3333 ) ; end of begin
3434 ) ; end of library
35-
Original file line number Diff line number Diff line change 2121 hash-table-ref/default hash-table-set! hash-table-delete! hash-table-intern!
2222 hash-table-update! hash-table-update!/default hash-table-pop! hash-table-clear!
2323 hash-table-size hash-table-keys hash-table-values hash-table-entries hash-table-find
24- hash-table-count hash-table-for-each hash-table-map->list hash-table->alist)
24+ hash-table-count hash-table-fold hash-table-for-each hash-table-map->list
25+ hash-table->alist)
2526 (begin
2627
2728 (define (assert-hash-table-type ht f )
128129 (typed-lambda ((pred? procedure?) (ht hash-table?))
129130 (count (lambda (x ) (pred? (car x) (cdr x))) (map values ht))))
130131
132+ (define (hash-table-fold proc seed ht )
133+ (assert-hash-table-type ht hash-table-fold)
134+ (let ((result seed))
135+ (hash-table-for-each
136+ (lambda (k v )
137+ (set! result (proc k v result)))
138+ ht)
139+ result))
140+
131141 (define hash-table-for-each
132142 (typed-lambda ((proc procedure?) (ht hash-table?))
133143 (for-each (lambda (x ) (proc (car x) (cdr x))) ht)))
Original file line number Diff line number Diff line change 256256 (hash-table 'a 1 'b 2 'c 3 ))
257257 (check cnt => 6 ))
258258
259+ (let ((ht (hash-table 'a 1 'b 2 'c 3 )))
260+ (check (hash-table-fold (lambda (k v acc ) (+ acc v)) 0 ht) => 6 )
261+ )
262+
263+ (check (hash-table-fold (lambda (k v acc ) (+ acc v)) 10 (hash-table)) => 10 )
264+
259265(let* ((ht (hash-table 'a 1 'b 2 'c 3 ))
260266 (ks (hash-table-map->list (lambda (k v ) k) ht))
261267 (vs (hash-table-map->list (lambda (k v ) v) ht)))
275281 => (list 'k1 'v1 ))
276282
277283(check-report)
278-
You can’t perform that action at this time.
0 commit comments