在 Erlang 中是否有更简单的方法来修改 subsubsub 记录字段中的值?

发布于 2024-07-14 10:49:28 字数 973 浏览 5 评论 0原文

所以我有一个相当深的记录定义层次结构:

-record(cat,            {name = '_',           attitude = '_',}).
-record(mat,            {color = '_',          fabric = '_'}).
-record(packet,         {cat = '_',            mat = '_'}).
-record(stamped_packet, {packet = '_',         timestamp = '_'}).
-record(enchilada,  {stamped_packet = '_', snarky_comment = ""}).

现在我有一个玉米卷饼,我想制作一个新的 就像它一样,除了 subsubsubrecords 之一的值。 这就是我一直在做的事情。

update_attitude(Ench0, NewState)
  when is_record(Ench0, enchilada)->

    %% Pick the old one apart.
    #enchilada{stamped_packet     = SP0} = Ench0,
    #stamped_packet{packet = PK0} = SP0,
    #packet{cat = Tag0}    = PK0,

    %% Build up the new one.
    Tude1 = Tude0#cat{attitude = NewState},
    PK1 = PK0#packet{cat = Tude1},
    SP1 = SP0#stamped_packet{packet = PK1},

    %% Thank God that's over.
    Ench0#enchilada{stamped_packet = SP1}.

光是想到这一点就让人痛苦。 有没有更好的办法?

So I've got a fairly deep hierarchy of record definitions:

-record(cat,            {name = '_',           attitude = '_',}).
-record(mat,            {color = '_',          fabric = '_'}).
-record(packet,         {cat = '_',            mat = '_'}).
-record(stamped_packet, {packet = '_',         timestamp = '_'}).
-record(enchilada,  {stamped_packet = '_', snarky_comment = ""}).

And now I've got an enchilada, and I want to make a new one that's
just like it except for the value of one of the subsubsubrecords.
Here's what I've been doing.

update_attitude(Ench0, NewState)
  when is_record(Ench0, enchilada)->

    %% Pick the old one apart.
    #enchilada{stamped_packet     = SP0} = Ench0,
    #stamped_packet{packet = PK0} = SP0,
    #packet{cat = Tag0}    = PK0,

    %% Build up the new one.
    Tude1 = Tude0#cat{attitude = NewState},
    PK1 = PK0#packet{cat = Tude1},
    SP1 = SP0#stamped_packet{packet = PK1},

    %% Thank God that's over.
    Ench0#enchilada{stamped_packet = SP1}.

Just thinking about this is painful. Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情释 2024-07-21 10:49:28

正如 Hynek 所建议的,您可以删除临时变量并执行以下操作:

update_attitude(E = #enchilada{stamped_packet = (P = #packet{cat=C})},
                NewAttitude) ->
    E#enchilada{stamped_packet = P#packet{cat = C#cat{attitude=NewAttitude}}}.

Yariv Sadan 对同样的问题感到沮丧并写道 鲁莽记录的类型推断解析转换 允许您编写:

-compile({parse_transform, recless}).

update_attitude(Enchilada = #enchilada{}, Attitude) ->
    Enchilada.stamped_packet.packet.cat.attitude = Attitude.

As Hynek suggests, you can elide the temporary variables and do:

update_attitude(E = #enchilada{stamped_packet = (P = #packet{cat=C})},
                NewAttitude) ->
    E#enchilada{stamped_packet = P#packet{cat = C#cat{attitude=NewAttitude}}}.

Yariv Sadan got frustrated with the same issue and wrote Recless, a type inferring parse transform for records which would allow you to write:

-compile({parse_transform, recless}).

update_attitude(Enchilada = #enchilada{}, Attitude) ->
    Enchilada.stamped_packet.packet.cat.attitude = Attitude.
恋你朝朝暮暮 2024-07-21 10:49:28

试试这个:

update_attitude(E = #enchilada{
    stamped_packet = (SP = #stamped_packet{
      packet = (P = #packet{
        cat = C
    })})}, NewState) ->
    E#enchilada{
      stamped_packet = SP#stamped_packet{
        packet = P#packet{
          cat = C#cat{
            attitude = NewState
    }}}}.

无论如何,结构并不是 Erlang 最强大的部分。

Try this:

update_attitude(E = #enchilada{
    stamped_packet = (SP = #stamped_packet{
      packet = (P = #packet{
        cat = C
    })})}, NewState) ->
    E#enchilada{
      stamped_packet = SP#stamped_packet{
        packet = P#packet{
          cat = C#cat{
            attitude = NewState
    }}}}.

anyway, structures is not most powerful part of Erlang.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文