[erlang-questions] Potential EEP for binary comprehension addition

Jay Nelson <>
Mon Aug 13 16:19:22 CEST 2007


Binary comprehensions nicely parallel list comprehensions and add the  
ability to create a list or binary by visiting subsequences of a  
binary and transform and/or filter them.

Binaries have a positional feature that can be leveraged in a way  
that list positions do not offer: it is highly efficient to index or  
extract a subsequence from a binary.  Positional information can also  
be useful for things like indexing an external file.

I haven't been able to come up with a good syntax -- the only thing I  
can think of is magic variables ala Perl -- but it would be nice to  
be able to reference positional information during the comprehension.

##PA is the absolute position in a binary that matches.
##PR is the relative position in a binary that matches.

Binary = <<"abcd abcd abcd">>,
Positions = [ ##PA || X:8 <<- Binary, X == $a ],
Positions = [ 0, 5, 10 ],
Offsets = [ ##PR l| X:8 <<-Binary, X == $a ].
Offsets = [ 0, 5, 5 ].

##PA is useful for absolute offsets to a bit stream or a file read as  
binary or text.
##PR is useful when clipping out sub-binaries after filtering, or for  
re-encoding the binary in slices with run-lengths preceding each slice.

Any suggestions on syntax or comments on the features are welcome.

jay




More information about the erlang-questions mailing list