[erlang-questions] Binary comprehension (was Re: double to float)

Zvi <>
Fri Oct 19 18:22:38 CEST 2007


Found the answer:
---------------------------------------
bc.erl file:
--------------------------------------
-module(bc).
-compile([binary_comprehension]).
-export([test/0]).

test() -> 
	Bin = <<"hello">>,
	[B || <<B:8>> <= Bin].
---------------------------------------
1> c(bc).        
{ok,bc}
2> bc:test().
"hello"

 OR:

---------------------------------------
bc.erl file:
--------------------------------------
-module(bc).
-export([test/0]).

test() -> 
	Bin = <<"hello">>,
	[B || <<B:8>> <= Bin].
---------------------------------------
1> c(bc,[binary_comprehension]).
{ok,bc}
2> bc:test().                   
"hello"
---------------------------------------

Still doesn't know, how to run this from shell.
Also, too bad we can't write something like this:

  <<B:8 || B<-[1,2,3]>>.

output should be:

  <<1,2,3>>

Zvi



Zvi wrote:
> 
> 1> Bin = <<"hello">>.
> <<hello>>
> 2> [B || <<B:8>> <= Bin].
> ** 1: binary generator in code not compiled with the binary comprehension
> flag *
> 
> How do I enable binary comprehension?
> Do I really need to recompile entire Erlang/OTP with this?
> Or does it referes to the compile directive in the .erl source, something
> like this?
> 
> -compile([binary_comprehension]).
> 
> or
> 
> c(mod.erl, [binary_comprehension]).
> 
> If yes, then how do I use it from the Erlang shell?
> 
> TIA,
> Zvi
> 
> 
> 
> 
> 
> 
> 
> 
> Matthew O'Gorman wrote:
>> 
>> this works great if you compile with binary_comprehension
>> 
>> List_of_floats = [Float || << Float/float >> <= Binary].
>> 
>> mog
>> On 10/16/07, Sten Kvamme <> wrote:
>>> Yes thanks, but I have the file in a binary variable and would like to
>>> do
>>> something like this:
>>>
>>> {<<F/float>>,B} = split_binary(Thefile, 8),
>>>
>>> or maybe
>>>
>>> <<F:8/binary,B/binary>> = Thefile,
>>> <<X/float>> =  F,
>>>
>>>
>>> /Sten
>>>
>>>
>>> On Oct 16, 2007, at 20:43 , Vlad Dumitrescu wrote:
>>>
>>>
>>> <<X/float>> = <<184,162,71,68,75,65,104,64>>
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> 
>>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>>
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/double-to-float-tf4635969.html#a13286772
Sent from the Erlang Questions mailing list archive at Nabble.com.




More information about the erlang-questions mailing list