1 API
port->list
port->bytes
port->string
port->string-list
post->sexp-list
port-fold
Version: 4.1.1.3

Port: useful functions for operating on ports

by Noel Welsh (noelwelsh at GMail)

The Port library contains useful functions for operating on ports, based on those provided by Scsh.

1 API

 (require (planet schematics/port:1))

(port->list reader [port])  (listof any)
  reader : (-> input-port? any)
  port : input-port? = (current-input-port)

Applys reader to the port, accumulating results in a list. If no input-port is given the value of current-input-port is used.

(port->bytes [port])  bytes?
  port : input-port? = (current-input-port)

Returns a bytes string containing all bytes read from the port. Reading stops when the port returns the eof-object. If no input-port is given the value of current-input-port is used.

(port->string [port])  string?
  port : input-port? = (current-input-port)

Returns a string containing all characters read from the port. Reading stops when the port returns the eof-object. Data is converted from bytes using the UTF-8 conversion. I.e. this function is equivalent to

  (bytes->string/utf-8 (read-bytes port))

If no input-port is given the value of current-input-port is used.

(port->string-list [port])  (listof string?)
  port : input-port? = (current-input-port)

Returns a list of all lines from the port (as returned by read-line). Reading stops when the port returns the eof-object. If no input-port is given the value of current-input-port is used.

(post->sexp-list [port])  (listof any)
  port : input-port? = (current-input-port)

Returns a list of all data read from the port (using the read function). Reading stops when the port returns the eof-object. If no input-port is given the value of current-input-port is used.

(port-fold port reader op seed ...)  
'b ...
  port : input-port?
  reader : (->* (input-port?) ('a 'b ...))
  op : (->* ('a 'b ...) (b'...))
  seed : 'b

To quote the Scsh manual: "This procedure can be used to perform a variety of iterative operations over an input stream. It repeatedly uses reader to read an object from port. If the first read returns eof, then the entire port-fold operation returns the seeds as multiple values. If the first read operation returns some other value v, then op is applied to v and the seeds: (op v . seeds). This should return a new set of seed values, and the reduction then loops, reading a new value from the port, and so forth. (If multiple seed values are used, then op must return multiple values.)"