I have Clojure function that takes a sequence of numbers chops it into the appropriate number of bits and returns a lazy sequence of the chunks (lowest order bits first). It pads the high order bits of the last block to fill out the block size and I need advice on the "best way(tm)" to record the amount of padding while keeping it lazy and functional?
Words of wisdom much appreciated.
(defn block-seq ([block-size bytes] "reads a byte-seq into a sequence of block-size bits." (block-seq 8 block-size bytes)) ([in-block-size out-block-size bytes] "converts a seq from in-block-size to out-block-size" ...
Parameters:
- in-block-size is the number of significant bits in each number in the input sequence
- out-block-size is the number of significant bits in each of the numbers in the lazy seq that is returned.
- bytes is a lazy sequence of numbers from which to extract bits
Here is an example that takes a sequence of three bytes and breaks it up into a sequence of two twenty bit numbers (and then prints it as a binary string).
user> (map #(java.lang.Integer/toBinaryString %) (block-seq 20 [0xAA 0xAA 0xAA])) ("10101010101010101010" "1010") user>
The second number in the sequence of 20 bit numbers has only four significant bits and has an effective 16 zeros added. If i then passed this sequence to another function that wanted to do something with the sequence and send it over a network; the code on the receiving end needs to know not to print/store/etc the last 16 bits.
PS: these can be chained. (block-seq 20 15 (block-seq 8 20 (read-bytes-from-file)))