Filling binary block lazy sequences

I have a Clojure function that takes a sequence of numbers, selects it into the appropriate number of bits, and returns a lazy sequence of pieces (lower bits first). It fills the upper order bit of the last block to fill in the block size, and I need advice on the "best path (tm)" to write down the number of indents, keeping it lazy and functional?

Words of wisdom are greatly 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"
   ...

Options:

  • in-block-size - 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 bits are extracted

Here is an example that takes a sequence of three bytes and splits it 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]))
 ("1010101010101010101010" "1010")
 user> 

The second number in a sequence of 20-bit numbers has only four significant bits and has 16 added 16 zeros. If I passed this sequence to another function that wanted to do something with the sequence and send it over the network; the code on the receiving side should know not to print / store / etc the last 16 bits.

PS: they can be connected in chains. (block-seq 20 15 (block-seq 8 20 (read-bytes-from-file)))

+4
source share
1 answer

It is not yet clear what you want to do, but it seems to you that you want to know how the best block-seq block can return the number of bits filled in the last fragment. Of course, this is not possible if you want to be lazy, so the number must be returned with the last piece or after it.

Without using metadata, you can simply return a list, e.g.

(1 2 3 :pad 12) 

Using metadata, you can add this additional information to the last minuses (Clojure cannot add metadata to integers), so the last minuses will be equivalent

 (with-meta '(3) {:pad 12}) 

To get it working, the binary block must be aware of this additional information in both cases in order to be able to unlock and then redo the last fragment.

How to transfer additional information on the wire, this is another question.

+1
source

Source: https://habr.com/ru/post/1286633/


All Articles