[erlang-questions] Missing binary BIFs

Loïc Hoguin <>
Thu Feb 9 12:57:28 CET 2012


Oh, didn't think it accepted nested lists. That's good to know, thanks.

And yeah I'm not interested in the structure, just the representation 
when printed as text.

On 02/09/2012 12:54 PM, Robert Raschke wrote:
> ()6> list_to_binary([1, [2], 3]).
> <<1,2,3>>
> ()7> binary_to_list(list_to_binary([1, [2], 3])).
> [1,2,3]
> ()8>
>
> But I see you are using iolist, so you are probably not interested in
> nested lists keeping their structure.
>
> On Thu, Feb 9, 2012 at 11:51 AM, Loïc Hoguin <
> <mailto:>> wrote:
>
>     Hm I would be very surprised if binary_to_list(list_to_binary(__L))
>     /= L wouldn't be true. Do you have an example of this?
>
>
>     On 02/09/2012 12:48 PM, Robert Raschke wrote:
>
>         Sure, but don't you usually use them in pairs? Just wondering cause
>         list_to_binary/1 and binary_to_list/1 are not opposites of each
>         other,
>         that is, binary_to_list(list_to_binary(__L)) /= L in general.
>         But I'm
>         guessing that probably doesn't matter for your use case.
>
>         On Thu, Feb 9, 2012 at 11:31 AM, Loïc Hoguin <
>         <mailto:>
>         <mailto: <mailto:>>> wrote:
>
>             Those encode the term in the Erlang term format, which has a
>         very
>             particular purpose. The functions I'm talking about are just
>             conversion from one type to binary strings.
>
>
>             On 02/09/2012 12:29 PM, Robert Raschke wrote:
>
>                 Are you guys sure you're not looking for
>         term_to_binary/1 and
>                 binary_to_term/1 ?
>
>                 On Thu, Feb 9, 2012 at 10:27 AM, Loďc Hoguin
>         < <mailto:>
>         <mailto: <mailto:>>
>         <mailto: <mailto:>
>         <mailto: <mailto:>>>> wrote:
>
>                     While seemingly practical, I'm not sure why I would
>         want any
>                 place
>                     in my code where I wouldn't be sure what the input
>         type is. Even
>                     with dialyzer checks I'm sure I would make false
>         assumptions
>                 and get
>                     surprises here and there.
>
>                     I also would like an efficient solution, both in
>         processing
>                 speed
>                     and memory use. Converting to list then binary isn't
>         that great,
>                     even less so after module:function calls with guards.
>
>
>                     On 02/09/2012 09:03 AM, Rapsey wrote:
>
>                         I wrote functions that convert between all types
>         that I use
>                         constantly.
>                         Much easier to just force a specific type
>         somewhere if
>                 you're
>                         not sure
>                         what the input type is going to be instead of
>         doing checks
>                         everywhere or
>                         annoying conversions like the one you wrote.
>                         It has a dependency on mochinum:digits/1 though.
>
>                         tolist(<<_/binary>> = P) ->
>                         binary_to_list(P);
>                         tolist(P) when is_atom(P) ->
>                         atom_to_list(P);
>                         tolist(P) when is_integer(P) ->
>                         integer_to_list(P);
>                         tolist(P) when is_float(P) ->
>                         mochinum:digits(P);
>                         tolist(P) when is_list(P) ->
>                         P.
>
>                         tobin(<<_/binary>> = P) ->
>                         P;
>                         tobin(P) when is_list(P) ->
>                         iolist_to_binary(P);
>                         tobin(P) when is_atom(P) ->
>                         atom_to_binary(P,latin1);
>                         tobin(P) when is_integer(P) ->
>                         tobin(integer_to_list(P));
>                         tobin(P) when is_float(P) ->
>                         tobin(mochinum:digits(P)).
>
>                         toatom(P) when is_binary(P) ->
>                         binary_to_atom(P,latin1);
>                         toatom(P) when is_list(P) ->
>                         list_to_atom(P);
>                         toatom(P) when is_atom(P) ->
>                         P.
>                         toint(<<_/binary>> = P) ->
>                         list_to_integer(binary_to_______list(P));
>
>
>                         toint([_|_] = P) ->
>                         list_to_integer(P);
>                         toint(P) when is_integer(P) ->
>                         P;
>                         toint(P) when is_float(P) ->
>                         erlang:round(P).
>                         tofloat(P) when is_integer(P) ->
>                         P / 1;
>                         tofloat(P) when is_float(P) ->
>                         P;
>                         tofloat(P) when is_binary(P) ->
>                         tofloat(binary_to_list(P));
>                         tofloat(P) when is_list(P) ->
>                         Str = string:join(string:tokens(P,",______"),"."),
>
>
>                         case string:str(Str,".") of
>                         0 ->
>                         tofloat(P ++ ".0");
>                         _ ->
>                         list_to_float(Str)
>                         end;
>                         tofloat(P) ->
>                         list_to_float(tolist(P)).
>
>
>                         On Wed, Feb 8, 2012 at 7:29 PM, Loďc Hoguin
>         < <mailto:>
>         <mailto: <mailto:>>
>         <mailto: <mailto:>
>         <mailto: <mailto:>>>
>
>         <mailto: <mailto:>
>         <mailto: <mailto:>>
>         <mailto: <mailto:>
>         <mailto: <mailto:>>>>> wrote:
>
>                             Hello,
>
>                             It feels a bit tedious to keep doing things like
>                               list_to_binary(integer_to_________list(42))
>
>                             or even
>
>           list_to_binary(atom_to_list(________forty_two))
>
>
>
>
>                             when we could have convenient
>         integer_to_binary/1 or
>                 /2 or
>                             atom_to_list/1 BIFs.
>
>                             Is there any chance to add them in a future
>         release?
>                 With
>                         all the
>                             advantages of binaries it's a shame it's not
>         easier
>                 to deal
>                         with them.
>
>                             Thanks.
>
>                             --
>                             Loďc Hoguin
>                             Erlang Cowboy
>                             Nine Nines
>
>           _______________________________________________________
>
>
>                             erlang-questions mailing list
>          <mailto:>
>         <mailto:
>         <mailto:>>
>         <mailto:erlang-questions@
>         <mailto:erlang-questions@>__erl__ang.org <http://erlang.org>
>         <mailto:
>         <mailto:>>>
>         <mailto:erlang-questions@ <mailto:erlang-questions@>
>         <mailto:erlang-questions@
>         <mailto:erlang-questions@>>__er__l__ang.org
>         <http://erl__ang.org> <http://erlang.org>
>         <mailto:erlang-questions@
>         <mailto:erlang-questions@>__erl__ang.org <http://erlang.org>
>         <mailto:
>         <mailto:>>>>
>         http://erlang.org/mailman/________listinfo/erlang-questions
>         <http://erlang.org/mailman/______listinfo/erlang-questions>
>         <http://erlang.org/mailman/______listinfo/erlang-questions
>         <http://erlang.org/mailman/____listinfo/erlang-questions>>
>
>         <http://erlang.org/mailman/______listinfo/erlang-questions
>         <http://erlang.org/mailman/____listinfo/erlang-questions>
>         <http://erlang.org/mailman/____listinfo/erlang-questions
>         <http://erlang.org/mailman/__listinfo/erlang-questions>>>
>
>         <http://erlang.org/mailman/______listinfo/erlang-questions
>         <http://erlang.org/mailman/____listinfo/erlang-questions>
>         <http://erlang.org/mailman/____listinfo/erlang-questions
>         <http://erlang.org/mailman/__listinfo/erlang-questions>>
>         <http://erlang.org/mailman/____listinfo/erlang-questions
>         <http://erlang.org/mailman/__listinfo/erlang-questions>
>         <http://erlang.org/mailman/__listinfo/erlang-questions
>         <http://erlang.org/mailman/listinfo/erlang-questions>>>>
>
>
>
>
>                     --
>                     Loďc Hoguin
>
>                     Erlang Cowboy
>                     Nine Nines
>                     _____________________________________________________
>                     erlang-questions mailing list
>          <mailto:>
>         <mailto:
>         <mailto:>>
>         <mailto:erlang-questions@
>         <mailto:erlang-questions@>__erl__ang.org <http://erlang.org>
>         <mailto:
>         <mailto:>>>
>         http://erlang.org/mailman/______listinfo/erlang-questions
>         <http://erlang.org/mailman/____listinfo/erlang-questions>
>         <http://erlang.org/mailman/____listinfo/erlang-questions
>         <http://erlang.org/mailman/__listinfo/erlang-questions>>
>         <http://erlang.org/mailman/____listinfo/erlang-questions
>         <http://erlang.org/mailman/__listinfo/erlang-questions>
>         <http://erlang.org/mailman/__listinfo/erlang-questions
>         <http://erlang.org/mailman/listinfo/erlang-questions>>>
>
>
>
>
>                 ___________________________________________________
>                 erlang-questions mailing list
>          <mailto:>
>         <mailto:
>         <mailto:>>
>         http://erlang.org/mailman/____listinfo/erlang-questions
>         <http://erlang.org/mailman/__listinfo/erlang-questions>
>         <http://erlang.org/mailman/__listinfo/erlang-questions
>         <http://erlang.org/mailman/listinfo/erlang-questions>>
>
>
>
>             --
>             Loďc Hoguin
>             Erlang Cowboy
>             Nine Nines
>
>
>
>
>         _________________________________________________
>         erlang-questions mailing list
>          <mailto:>
>         http://erlang.org/mailman/__listinfo/erlang-questions
>         <http://erlang.org/mailman/listinfo/erlang-questions>
>
>
>
>     --
>     Loïc Hoguin
>     Erlang Cowboy
>     Nine Nines
>
>


-- 
Loïc Hoguin
Erlang Cowboy
Nine Nines



More information about the erlang-questions mailing list