或类似的功能,以防万一的声明?或者也许是更通用的模式?
我已经学习 Erlang 一段时间了,为了学习它,我正在编写一个 IRC 机器人。该 IRC 机器人应该侦听“!command”和“Nick: command”形式的命令。我预先解析了 IRC 协议,以便我只需匹配发送消息。我正在使用这样的二进制模式来执行此操作:
case Msg of
[Nick, _, <<"PRIVMSG">>, <<"#",Channel/binary>>, <<"!rock">>] ->
irckybot_api:privmsg(<<"#",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
我必须为此编写两个模式,对吗?我不能将两种模式合并为一种吗?也许有更通用的模式?我真的不知道……
LG, CK
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(<<"#",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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想它可以更好地写成:
I guess it could be better written as :