[erlang-questions] map over bitstring

Raimo Niskanen raimo+erlang-questions@REDACTED
Fri Oct 22 09:49:45 CEST 2010


On Fri, Oct 22, 2010 at 12:55:06AM -0400, rgowka1 wrote:
> Thanks, that was really helpful.
> 
> One more question, how do I zip on two bitstrings.

For that you need to write a function of your own, matching
out the heads of both bitstrings and building a new by appending.

Uncompiled:
zip(A, B) -> zip(A, B, <<>>).
zip(<<X,A/binary>>, <<Y,B/binary>>, R) ->
    zip(A, B, <<R/binary,X,Y>>);
zip(<<>>, <<>>, R) -> R.

The comprehensions can not do it since they do all combinations
of the generators and do not do them in parallel.

> 
> On 10/21/10, Jesper Louis Andersen <jesper.louis.andersen@REDACTED> wrote:
> > On Thu, Oct 21, 2010 at 9:56 PM, rgowka1 <rgowka1@REDACTED> wrote:
> >> Hi -
> >>
> >> How do I map a function over a bitstring without converting into string or
> >> list.
> >>
> >> For example - how can I replace << "a" >> with << "X" >> and any other
> >> character to << "Y" >>??
> >>
> >> InBin = << "abcaa" >>
> >>
> >> OutBin = << "XYYXX" >>
> >
> > The trick is Binary Comprehensions or by using the (R14B) binary
> > module. Here is a solution using the Binary Comprehensions:
> >
> > 3> << <<case X of $a -> $X; _ -> $Y end>> || <<X>> <= <<"abcaa" >> >>.
> > <<"XYYXX">>
> >
> > --
> > J.
> >
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list