stream.scm 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. ;;; guile-openai --- An OpenAI API client for Guile
  2. ;;; Copyright © 2023 Andrew Whatson <whatson@tailcall.au>
  3. ;;;
  4. ;;; This file is part of guile-openai.
  5. ;;;
  6. ;;; guile-openai is free software: you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU Affero General Public License as
  8. ;;; published by the Free Software Foundation, either version 3 of the
  9. ;;; License, or (at your option) any later version.
  10. ;;;
  11. ;;; guile-openai is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;; Affero General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU Affero General Public
  17. ;;; License along with guile-openai. If not, see
  18. ;;; <https://www.gnu.org/licenses/>.
  19. (define-module (openai utils stream)
  20. #:use-module (srfi srfi-41)
  21. #:export (stream-filter-map))
  22. (define (stream-filter-map proc strm)
  23. (stream-let loop ((strm strm))
  24. (if (stream-null? strm)
  25. stream-null
  26. (let ((result (proc (stream-car strm))))
  27. (if result
  28. (stream-cons result (loop (stream-cdr strm)))
  29. (loop (stream-cdr strm)))))))