Erlang futures

Robert Virding rv@REDACTED
Wed Apr 18 14:55:17 CEST 2001


James Hague <jamesh@REDACTED> writes:
>On that subject, here is a binary version of lists:splitwith, which I have
>found handy:
>
>% Split a binary into two parts and return {list, binary}.
>
>splitwith(Pred, B) ->
>   splitwith(Pred, B, []).
>splitwith(Pred, <<Hd,Tail/binary>>=B, Taken) ->
>   case Pred(Hd) of
>      true  -> splitwith(Pred, Tail, [Hd|Taken]);
>      false -> {lists:reverse(Taken), B}
>   end;
>splitwith(Pred, <<>>, Taken) ->
>   {lists:reverse(Taken),[]}.

To be truly a binary version it should return two binaries not a list
and binary.  I think the easiest and most efficient way would be:

splitwith(Pred, B) ->
    split_binary(B, splitwith(Pred, B, 0)).

splitwith(Pred, <<H:8,T/binary>>, I) ->
    case Pred(H) of
	true -> splitwith(Pred, T, I+1);
	false -> I
    end;
splitwith(Pred, <<>>, I) -> I.

For example this is also what is wrong with ets:filter/3, it returns a
list but it should really return a filtered ets table.

        Robert
--
Robert Virding                          Tel: +46 (0)8 545 55 017
Alteon Web Systems                      Email: rv@REDACTED
S:t Eriksgatan 44                       WWW: http://www.bluetail.com/~rv
SE-112 34 Stockholm, SWEDEN
"Folk säger att jag inte bryr mig om någonting, men det skiter jag i".



More information about the erlang-questions mailing list