array-helpers.scm 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. (library (array-helpers)
  2. (export array-len-in-dim
  3. arrays->hash-table)
  4. (import
  5. (except (rnrs base) let-values map error)
  6. (only (guile)
  7. lambda* λ
  8. current-output-port
  9. ;; arrays
  10. array-shape
  11. array-ref)
  12. ;; lists
  13. (srfi srfi-1)
  14. ;; hash tables
  15. (srfi srfi-69))
  16. (define array-len-in-dim
  17. (λ (arr dim)
  18. (let* ([shape (array-shape arr)]
  19. [dim-min-max (list-ref shape dim)])
  20. (+ (- (second dim-min-max)
  21. (first dim-min-max))
  22. 1))))
  23. (define arrays->hash-table
  24. (lambda* (keys-arr vals-arrs #:optional (equal-func equal?))
  25. (let ([rows (array-len-in-dim keys-arr 0)]
  26. [cols (array-len-in-dim keys-arr 1)]
  27. [table (make-hash-table equal-func)])
  28. (let iter-rows ([row-ind 0])
  29. (let iter-cols ([col-ind 0])
  30. (cond
  31. [(< row-ind rows)
  32. (cond
  33. [(< col-ind cols)
  34. (hash-table-set! table
  35. (array-ref keys-arr row-ind col-ind)
  36. (array-ref vals-arrs row-ind col-ind))
  37. (iter-cols (+ col-ind 1))]
  38. [else (iter-rows (+ row-ind 1))])]
  39. [else table])))))))