I am writing a Haskell function that operates on a list of ByteString
values. I need to do a different operation on the first and last items (which may be the same if the list has only one item).
Specifically, I want to write out the following:
"item-1\
\item-2\
\item-3\
...
\item-n"
where item-1
starts with a double quote and ends with a backslash and item-n
starts with a backslash and ends with a double quote. All items between item-1
and item-n
start and end with backslashes. I am emitting a base64 encoded value as a Haskell String
for some code generation. I have already broken the original (long) base64-encoded ByteString
into chunks that are 64 characters long each.
I just realized that my question is a dumb one.
I can just use intercalate
to inject the "\\\n\\"
between the items and then prepend and append a double quote:
import qualified Data.ByteString.Lazy.Char8 as L
(L.pack "\"") `L.append` (L.intercalate "\\\n\\" items) `L.append` (L.pack "\"")
Sample output:
"H4sIAAAAAA\
\AAA2NgAAMm\
\CMXA7JRYxI\
\Am5JafD2Uy\
\AgDvdHs6Lg\
\AAAA==\
\"
You can also consider splitting your list using:
So [head a] ++ init (tail a) ++ [last a] == a
.
That way, you can change the first and last elements of the list individually and map a function over the "init" part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With