开发者

or-like feature in case … of statement? Or maybe a more generic pattern?

开发者 https://www.devze.com 2023-03-22 21:06 出处:网络
I\'ve been learning Erlang for a while now, and for to learn it I\'m writing a IRC bot. This IRC bot should listen to commands in the „!command“ and the „Nick: command“ form. I pre-parse the IRC p

I've been learning Erlang for a while now, and for to learn it I'm writing a IRC bot. This IRC bot should listen to commands in the „!command“ and the „Nick: command“ form. I pre-parse the IRC protocol so that I have to match the send message only. I'm doing this with binary patterns like this:

case Msg of
    [Nick, _, <<"PRIVMSG">>, <<"#",Channel/binary>>, <<"!rock">>] ->
        irckybot_api:privmsg(开发者_Go百科<<"#",Channel/binary>>, [Nick, choose_hand(rock)]),
        {ok, State};
    [Nick, _, <<"PRIVMSG">>, <<"#",Channel/binary>>, <<BNick:Len/binary,": rock">>] ->
        irckybot_api:privmsg(<<"#",Channel/binary>>, [Nick, choose_hand(rock)]),
        {ok, State};
end

Am I right that I have to write two patterns for this? Can't I consolidate the two patterns to one? Maybe with a more generic pattern? I don't really know…

LG, CK


I guess it could be better written as :

case Msg of
  [Nick, _, <<"PRIVMSG">>, <<"#",Channel/binary>>, X] ->
    case X of
       <<"!rock">> -> ....;
       <<BNick:Len/binary,": rock">> -> .......
    end;
  _ -> .......
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号