statistics.scm 987 B

12345678910111213141516171819202122232425262728293031323334353637
  1. (library (statistics)
  2. (export do-n-times
  3. count-things)
  4. (import (except (rnrs base) error map)
  5. (only (guile)
  6. lambda* λ)
  7. ;; SRFI 1: list procedures
  8. ;; (srfi srfi-1)
  9. ;; SRFI 69: hash tables
  10. (srfi srfi-69))
  11. (define do-n-times
  12. (λ (proc n)
  13. (let loop ([n n] [results '()])
  14. (cond
  15. [(> n 0)
  16. (loop (- n 1) (cons (proc) results))]
  17. [else results]))))
  18. (define count-things
  19. (λ (things thing->hash-key count-proc init-count)
  20. (let ([count-table (make-hash-table)])
  21. (for-each
  22. (λ (thing)
  23. (let ([hash-key (thing->hash-key thing)])
  24. (hash-table-set!
  25. count-table
  26. hash-key
  27. (+ (hash-table-ref count-table
  28. hash-key
  29. (λ () init-count))
  30. (count-proc thing)))))
  31. things)
  32. count-table))))