port-utils.scm 970 B

12345678910111213141516171819202122232425262728293031323334353637
  1. (library (port-utils)
  2. (export
  3. port-u8-for-each)
  4. (import
  5. (except (rnrs base) let-values)
  6. (only (guile)
  7. lambda* λ
  8. error
  9. eof-object?
  10. simple-format
  11. current-output-port
  12. current-input-port
  13. call-with-output-string
  14. call-with-input-string
  15. call-with-output-file
  16. remainder
  17. array-for-each)
  18. (prefix (logging) log:)
  19. (ice-9 binary-ports)
  20. ;; Bytevectors are created by procedures from (ice-9 binary-ports), but to
  21. ;; work with them, other imports are needed.
  22. (rnrs bytevectors)
  23. (ice-9 textual-ports)))
  24. (define port-u8-for-each
  25. (λ (proc port)
  26. "proc is called as follows: (proc index-of-u8 u8)"
  27. (let iter ([results '()] [index 0] [u8 (get-u8 port)])
  28. (cond
  29. [(eof-object? u8) (reverse results)]
  30. [else
  31. (iter (cons (proc index u8) results)
  32. (+ index 1)
  33. (get-u8 port))]))))