[erlang-questions] (no subject)

Julian Fondren <>
Sun Dec 10 11:21:33 CET 2006

I have a binaries.erl that I've grown very slowly, as I come to want
functions.  I do a lot of personal work[0] on a very
memory-constrained[1] machine, so I probably have much more to do
with memory-saving techniques involving iterators and binaries than
most people.  In any case, although I normally care about strings,
with binaries.erl I have maintained some flexibility in unit sizes
-- for example, my reverse/1 simply reverses a binary by byte, but
reverse/2 also exists to reverse by N-bit-values.

So it disappoints me to discover that I can't continue this pattern
with my newest subseq/4 function:

  substr(B,I,L) -> subseq(8,B,I,L).
  subseq(N,B,I,L) ->
      N2 = I-1,
      <<_:N2/binary-unit:N, R:L/binary-unit:N, _/binary>> = B,

Which flexibility would allow subseq(4,<<"hi">>,2,2) to return <<134>>.

But only literal values can appear as a unit size.

Is this restriction likely to go away?  Why does it exist?[2]


0] pretty much everything, as Erlang is almost unique in that it runs
   perfectly acceptably on this machine, whereas the supplied Perl has
   a hideous start-up time, whereas many other language implementations
   won't even compile.  I have an erlang node running all the time to
   manage my *music*, of all things, as mplayer has also has a hideous
   start-up time -- but will respond with alacrity to piped commands.

1] OpenBSD on a Zaurus SL-C3200, with 64MB of RAM.

2] OK, with some more thought, I see that I can do this:

     subseq(N,B,I,L) ->
         H = (I - 1) * N,
         L2 = L * N,
         F = (H + L2) rem 8,  % alignment for _/binary
         <<_:H, R:L2, _:F, _/binary>> = B,

   but this is pretty ugly.

More information about the erlang-questions mailing list