[erlang-questions] queue:split/2 unsafe!

Fred Hebert mononcqc@REDACTED
Tue Apr 9 19:25:16 CEST 2013


I would prefer to keep the current behaviour. Note that it's similar to
the one used for different data structures, such as lists or binaries:

1> lists:split(4, [1,2,3]).
** exception error: bad argument
     in function  lists:split/2
        called as lists:split(4,[1,2,3])
2> erlang:split_binary(<<1,2,3>>, 4).
** exception error: bad argument
     in function  split_binary/2
        called as split_binary(<<1,2,3>>,4)

Going out of bound on where you want to split is, in all senses of the
an error. It's not the same as say, functions where you split in two
lists according to a predicate, a pattern, or a regular expression,
which as far I'm aware, are the only splitting functions that allow to
have empty elements one side or the other.

Regards,
Fred.

On 04/09, Ivan Uemlianin wrote:
> Dear All
> 
> I've just discovered that queue:split(N, Queue) causes a crash if N
> is larger than Queue has items:
> 
>     1> self().
>     <0.37.0>
>     2> Q1 = queue:new().
>     {[],[]}
>     3> Q2 = queue:in(a, Q1).
>     {[a],[]}
>     4> Q3 = queue:in(b, Q2).
>     {[b],[a]}
>     5> Q4 = queue:in(c, Q3).
>     {[c,b],[a]}
>     6> queue:split(4, Q4).
>     ** exception error: bad argument
>          in function  queue:split/2
>             called as queue:split(4,{[c,b],[a]})
>     7> self().
>     <0.45.0>
> 
> I've written a safe split:
> 
>     queue_safe_split(N, Q) ->
>         case queue:len(Q) >= N of
>             true ->
>                 queue:split(N,Q);
>             false ->
>                 {Q, queue:new()}
>         end.
> 
> Thanks
> 
> Ivan
> 
> 
> -- 
> ============================================================
> Ivan A. Uemlianin PhD
> Llaisdy
> Speech Technology Research and Development
> 
>                     ivan@REDACTED
>                      www.llaisdy.com
>                          llaisdy.wordpress.com
>               github.com/llaisdy
>                      www.linkedin.com/in/ivanuemlianin
> 
>                         festina lente
> ============================================================
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list