Erlang风格 - case与函数模式匹配 [英] Erlang style - case vs function pattern matching

查看:262
本文介绍了Erlang风格 - case与函数模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经到了阶段,我已经写了相当多的Erlang代码,我可以看到一些风格(坏或好)爬上我的方式,我一直在写它。这个特殊的成语我想要一些意见 - 是更好的(更可读/更快/任何)转换案例样式语句到函数模式匹配?



p>

比较(一个假设的例子)

  case {Size& 100000,Type} of 
{true,ets} - >
%%做一些转换为dets
的东西;
{false,dets} - >
%%执行某些操作转换为ets
somethingelse;
_ - >
ignoreit
end;

  ... 
maybeChangeStorage(Size,Type)
...

maybeChangeStorage(Size,ets)when Size> 10000 - >
something;
maybeChangeStorage(Size,dets)when Size< 10000 - >
somethingelse;
maybeChangeStorage(_,_) - >
ignoreit。

我喜欢后者在大多数情况下,但我会对其他意见感兴趣。

解决方案

第二种是首选方法,特别是如果您可以将子句保持为一行:

  maybeCngStor(Sz,ets)when Sz> 10000  - >东西
maybeCngStor(Sz,dets)当Sz < 10000 - >东西
maybeCngStor(_,_) - >忽略它。

让它很容易阅读和理由。始终选择将来最容易阅读的样式。通常你会发现一组子句,其中一个是10行,其余的只有一行 - 将一个长的一个函数打开:

  maybeCngStor(Sz,ets)when Sz> 10000  - >东西
maybeCngStor(Sz,dets)当Sz < 10000 - > somethingelse();
maybeCngStor(_,_) - >忽略它。

somethingelse() - >
(...)
返回。

很少有事情像布置子句以使它们对齐并使用短变量名



一个好的技巧,如果你使用记录很多是匹配记录到短变量:

p>

  #record {foo = F,bar = B,baz = Bz} =参数

这给你简短的变量名称,当你从10,000英尺降落到这个函数寻找下一个圣诞节的错误。 F显然是一个Foo,等等...


I've got to the stage where I've written quite a bit of Erlang code now, and I can see some style (bad or good) creeping into the way I've been writing it. This particular idiom I'd like some opinion on - is it better (more readable/faster/whatever) to convert case style statements to function pattern matching?

E.g.

Compare (a contrived example)

case {Size > 100000, Type} of
    {true, ets } ->
         %% Do something to convert to dets
         something;
    {false, dets} ->
         %% do something to convert to ets
         somethingelse;
    _ ->
         ignoreit
end;

with

...
maybeChangeStorage(Size, Type)
...

maybeChangeStorage(Size, ets) when Size > 10000 ->
   something;
maybeChangeStorage(Size, dets) when Size < 10000 ->
   somethingelse;
maybeChangeStorage(_,_) ->
   ignoreit.

I prefer the latter in most cases but I'd be interested in other opinion.

解决方案

The second is the preferred way especially if you can keep the clauses to a single line:

maybeCngStor(Sz, ets)  when Sz > 10000 -> something;
maybeCngStor(Sz, dets) when Sz < 10000 -> somethingelse;
maybeCngStor(_,_)                      -> ignoreit.

Makes it very easy to read and reason about. Always choose the style that will be easiest to read in the future. Often you find a set of clauses where one is a 10 liner and the rest are one lines only - break out the long one to a function:

maybeCngStor(Sz, ets)  when Sz > 10000 -> something;
maybeCngStor(Sz, dets) when Sz < 10000 -> somethingelse();
maybeCngStor(_,_)                      -> ignoreit.

somethingelse() ->
   (...)
   Return.

Little things like laying out the clauses to align them and using short variable names matter - but don't fall into the trap of changing everything to P, Q, R.

A good trick if you use records a lot is to match out the records to short variables:

#record{foo = F, bar = B, baz = Bz} = Parameter

This gives you short variable names that make sense when you parachute into the function from 10,000 feet looking for a bug next Christmas. F obviously is a Foo, etc, etc...

这篇关于Erlang风格 - case与函数模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆