Home > Uncategorized > Note to myslf: there is bitstring comprehensions in Erlang

Note to myslf: there is bitstring comprehensions in Erlang

It easy to handle streams of bytes in Erlang. For example consider the following simple task:
given a stream consisting of 3-byte chunks we want to return a stream consisting of those 3-byte chunks whose first byte is zero.

This can be written in the following manner:

keep_0XX(Bin) -> << <<0:1,X:2>> || <<0:1,X:2>> <= Bin >>.

I like it, I hope you too.

[update]
Previously I thought it was undocumented feature of erlang. As Bryan kindly pointed out, one can find it documented here.
Thank you Bryan.

Categories: Uncategorized Tags:
  1. July 10th, 2009 at 09:12 | #1

    I wouldn’t exactly call them “undocumented”:
    http://erlang.org/doc/reference_manual/expressions.html#6.23

  1. No trackbacks yet.