I'm serializing data which may be an integer, an object (list) with other nested objects, and trying to make a choice as to what approach to be using. Of two, the first one is to create bytevectors recursively and copy them in the calling functions to a larger single bytevector; the second one is to use some kind of the stream I could write to. Eventually, notwithstanding the choice, I would be able to use the resulting binary array in any further processing which may happen to occur, e. g. I would compress the output data and send it via network or just write some parts of it to a file.
I'd like to stay functional enough (or completely) yet implementing the fast serializer. I'm using Racket, though any other Scheme implementation will also do.
Thank you.
UPDATE:
Following are the examples I've added after I found a solution so that users save some time searching the way as to how to write data :]
write-byte
and write-bytes
are of particular use when you need to write octets.
> (bytes? (with-output-to-bytes (lambda () (write-byte 42))))
#t
> (bytevector? (with-output-to-bytes (lambda () (write-byte 42))))
#t
> (bytevector->u8-list (with-output-to-bytes (lambda () (write-byte 42))))
{42}
> (bytes->list (with-output-to-bytes (lambda () (write-byte 42) (write-bytes (integer->integer-bytes #x101CA75 4 #f #t)))))
(42 1 1 202 117)