helpers.scm 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. (define-module (helpers))
  2. (use-modules
  3. ;; SRFI-69 for hash tables
  4. (srfi srfi-69))
  5. (define-public do-n-times
  6. (λ (proc n)
  7. (let loop ([n n] [results '()])
  8. (cond
  9. [(> n 0)
  10. (loop (- n 1) (cons (proc) results))]
  11. [else results]))))
  12. (define-public count-things
  13. (λ (things thing->hash-key count-proc init-count)
  14. (let ([count-table (make-hash-table)])
  15. (for-each
  16. (λ (thing)
  17. (let ([hash-key (thing->hash-key thing)])
  18. (hash-table-set!
  19. count-table
  20. hash-key
  21. (+ (hash-table-ref count-table hash-key (λ () init-count))
  22. (count-proc thing)))))
  23. things)
  24. count-table)))
  25. (define-public simple-count-things
  26. (λ (things thing->hash-key)
  27. (count-things things
  28. thing->hash-key
  29. (λ (thing) 1)
  30. 0)))
  31. (define-public hash-table-filter-fold
  32. (lambda* (tab filter-proc acc-proc proc init #:optional (skip-proc #f))
  33. (hash-table-fold
  34. tab
  35. (λ (key val acc)
  36. (cond
  37. [(filter-proc key val)
  38. (acc-proc acc (proc key val))]
  39. [skip-proc
  40. (skip-proc key val)]
  41. [else
  42. acc]))
  43. init)))
  44. (define-public if-val-eqv?-seek-debug!
  45. (λ (val seek debug-proc)
  46. (cond
  47. [(eqv? val seek)
  48. (debug-proc val)
  49. val]
  50. [else
  51. val])))