开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜