Hello,
I have a use case for reading binary data from a file
into a C equivalent structure. I read the binary date
into a byte array before processing.
I came up with a split-byte-array function for this:
(defn split-byte-array
"takes a byte-array and returns its various fields
as a list of sequences. the length of each field in
bytes is taken from the list lens
user=> (split-byte-array (range 10) [3 2 2 3])
[(0 1 2) (3 4) (5 6) (7 8 9)]"
([byte-array lens] (split-byte-array byte-array lens []))
([byte-array lens acc]
(if (empty? lens) acc
(let [[s r] (split-at (first lens) byte-array)]
(recur r (rest lens) (conj acc s))))))
As shown in documentation for the function, the
stream gets split up into 3 2 2 and 3 bytes in the
above case.
I was wondering if there is a better way to do this.
This reminds me of "partition" but that doesn't
support something like a vector of partition sizes.
This is a common use case for a handling a
binary dump of c structures.
Is there any better way to do it than above?
Is this a common enough case to have something
like (partition coll ns) in contrib or core?
Parth
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---