python(vs. erlang)学习笔记之五 —— 数据类型 & 数据结构之二

这部分主要介绍bit string/binary、reference、port identifier、pid、record。这些数据类型都是erlang的数据类型,python是没有这些数据类型的
[size=medium][b]1、bit string和binary[/b][/size]
binary是一种用来存储大量raw type数据的数据类型,binary存储数据比lists和tuples的空间效率高。如果binary可打印字符串,就会输出为字符串,否则就输出为整数序列。[url="http://gashero.yeax.com/?p=51#binary"]参考资料[/url]

29> <<67,68,69>>.
<<"CDE">>
32> <<67,68,69,1>>.
<<67,68,69,1>>
34> list_to_binary([<<1,2,3>>,2,3,<<3,4,5>>]).
<<1,2,3,2,3,3,4,5>>
36> B = term_to_binary({abc,"are",userful}).
<<131,104,3,100,0,3,97,98,99,107,0,3,97,114,101,100,0,7,
117,115,101,114,102,117,108>>
37> binary_to_term(B).
{abc,"are",userful}
38> X = 34.
34
39> Y = 45.
45
40> Z = 78.
78
41> <<X:3,Y:7,Z:6>>.
<<"KN">>
42> <<16#123456:32/big>>. %//完整的格式Value:Size/TypeSpecifierList
<<0,18,52,86>>

[size=medium][b]2、reference[/b][/size]
reference是一个全局唯一的erlang term,用erlang:make_ref()创建,一般创建后放在某些数据里,然后在后边用来比较相等性。比如客户端在跟服务器端通信的时候,可以把ref传到服务端,然后服务端处理完请求后再把ref传到客户端,客户端就可以确认是这次请求的响应了。或者[url="http://phper.sinaapp.com/?p=77"]用来生成uuid[/url]。[url="http://gashero.yeax.com/?p=66#id16"]参考资料[/url]

43> erlang:make_ref().
#Ref<0.0.0.150>

[size=medium][b]3、port identifier[/b][/size]
当erlang要跟非erlang语言写的程序通信的时候,我们可以把这个外部程序跑到erlang运行时系统之外,作为一个操作系统进程来跟erlang通信,这时候要通信的程序叫做connected process,会创建一个port,通过port跟外部进程通信。

start_port(ClientName) -> %//这个代码是不能直接跑的,是一个完整程序的片段
process_flag(trap_exit,true),
Port = open_port({spawn,"./chat"},[{packet,4}]),
loop(ClientName,Port).

[size=medium][b]4、pid[/b][/size]
pid即process identifier,可以理解为进程的句柄或引用

-module(area_server0). %//这些代码要放在一个文件里
-export([loop/0]).
loop() ->
receive
{rectangle, Width, Ht} ->
io:format("Area of rectangle is ~p~n" ,[Width * Ht]),
loop();
{circle, R} ->
io:format("Area of circle is ~p~n" , [3.14159 * R * R]),
loop();
Other ->
io:format("I don't know what the area of a ~p is ~n" ,[Other]),
loop()
end.
1> Pid = spawn(fun area_server0:loop/0).
<0.36.0>
2> Pid ! {rectangle, 6, 10}.
Area of rectangle is 60
{rectangle,6,10}
3> Pid ! {circle, 23}.
Area of circle is 1661.90
{circle,23}
4> Pid ! {triangle,2,4,5}.
I don't know what the area of a {triangle,2,4,5} is
{triangle,2,4,5}

[size=medium][b]5、record[/b][/size]
record是用来存储一定数量元素的数据结构,这类似于c语言的struct结构;record不是真正的数据类型,在编译时,它会被翻译成tuple表达式

-record(person,
{
name = "yymt",
age = 26,
gender = m,
address = beijing,
home
}).%//放在record.hrl文件里

10> rr("record.hrl").
12> X = #person{}.
#person{name = "yymt",age = 26,gender = m,address = beijing,
home = undefined}
13> X1 = #person{home=henan,address=shenzhen}.
#person{name = "yymt",age = 26,gender = m,
address = shenzhen,home = henan}
14> #person{name=Who,age=Age} = X1.
#person{name = "yymt",age = 26,gender = m,
address = shenzhen,home = henan}
15> Who.
"yymt"
16> Age.
26

[size=medium][b]6、比较不同数据类型变量的大小[/b][/size]
[b]6.1、erlang[/b]

number < atom < reference < fun < port < pid < tuple < list < binary

这什么意思呢?看段代码便知:

17> 2 < a.
true
19> a < {}.
true

[b]6.2、python[/b]
python中set是不能跨数据类型比较的

boolean < number < dictionary < tuple < fun < list < string

>>> f = lambda x: x > 2
>>> {} < []
True
>>> 2 < {}
True
>>> 2 < '2'
True
>>> 2 < []
True
>>> 's' < []
False
>>> 2 < f
True
>>> 'a' < []
False
>>> [] < {}
False
>>> {} < []
True
>>> {'a'} < ['a']
Traceback (most recent call last):
File "<stdin>", line 1, in <modul
TypeError: can only compare to a set
>>> 'a' < [9]
False
>>> {'a':3} < ['a']
True
>>> 2 < {2:3}
True
>>> 'a' < (3)
False
>>> (3) < [3]
True
>>> {1:2} < (3)
False
>>> f < 3
False
>>> f < {1:2}
False
>>> f < [2]
True
>>> f < (2)
False
>>> 2 < True
False
>>> True < 2
True
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值