PasteRack.org
Paste # 22031
2018-08-01 23:02:25

Fork as a new paste.

Paste viewed 303 times.


Embed:

  1. #lang racket/base
  2.  
  3. ;; Example of using a pipe to create a modified input-port.
  4. ;; This example simply skips 255 bytes.
  5. (define (make-skipping-255-port in)
  6.   ;; Create a pipe, which has in and out ports.
  7.   (define-values (pin pout) (make-pipe))
  8.   ;; Racket has threads! Start one that reads from the original input,
  9.   ;; and writes to the pipe's output. It's important to close the
  10.   ;; output when done.
  11.   (thread (λ ()
  12.             ;; You could write this many ways, but the `in-port`
  13.             ;; sequence is handy here, as is using an #:unless clause
  14.             ;; with `for`.
  15.             (for ([b (in-port read-byte in)]
  16.                   #:unless (equal? b 255))
  17.               (write-byte b pout))
  18.             (close-output-port pout)))
  19.   ;; Return the input end of the pipe.
  20.   pin)
  21.  
  22. ;; Trying it out
  23. (define original-in (open-input-bytes (bytes 1 2 3 255 1 2 3)))
  24. (define skipping-in (make-skipping-255-port original-in))
  25. (for/list ([b (in-port read-byte skipping-in)])
  26.   b)

=>

'(1 2 3 1 2 3)