[erlang-questions] Doubt with lists:dropwhile

Jesse Gumm gumm@REDACTED
Wed Apr 25 19:49:19 CEST 2012


This is a misinterpretation of lists:dropwhile.

lists:dropwhile will remove elements of a list starting with the first
and iterate through the list, continuing to drop elements as long as
the predicate returns true.  However, as soon as the predicate returns
false, it'll just stop and return.

So in your case, the dropwhile didn't match the first element, so it
just returned.

Easy Example:

> LessThan5 = fun(X) X < 5 end.

> lists:dropwhile(LessThan5,[1,2,3,4,5,6]).
[5,6]

>lists:dropwhile(LessThan5,[6,5,4,3,2,1]).
[6,5,4,3,2,1]

-Jesse

On Wed, Apr 25, 2012 at 12:34 PM, Farruco Sanjurjo <madtrick@REDACTED> wrote:
> Hi,
>
> I'm not getting the expected result while using lists:dropwhile/2
>
> Here is an example of my problem:
>
> Data = <<"HTTP/1.1 205 Reset Content\r\n
>               Header-A: A\r\n
>               Header-C : dGhlIHNhbXBsZSBub25jZQ==\r\n
>               Header-D: D\r\n\r\n
>               ">>,
>
> lists:dropwhile(fun(E) -> E == <<>> end, binary:split(Data, <<"\r\n">>,
> [trim, global])).
>
> And this is the result:
>
> [<<"HTTP/1.1 205 Reset Content">>,
>  <<"\n              Header-A: A">>,
>  <<"\n              Header-C dGhlIHNhbXBsZSBub25jZQ==">>,
>  <<"\n              Header-D: D">>,<<>>,
>  <<"\n              ">>]
>
> But on the result above, there's a <<>> which if I understand the manpage of
> lists:dropwhile shouldn't be there.
>
> Am I misunderstanding something?
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



-- 
Jesse Gumm
Owner, Sigma Star Systems
414.940.4866 || sigma-star.com || @jessegumm



More information about the erlang-questions mailing list