在 Clojure 中,将用户输入字符串与长整型进行比较的好方法是什么?
我正在用 Clojure 构建一个纸牌游戏。牌有花色和分数。
{:suit 1 :score 9}
这些卡片是使用范围创建的,例如(范围suitTotal),因此:suit 和:score 的值的类别是Long。 玩家发送命令字符串,例如“discard1.9”是丢弃请求。 使用正则表达式来解析它:
(re-seq #"[0-9]+" command)
结果是字符串项“1”和“9”。使用这些结果创建的卡片将是
{:suit "1" :score "9"}
我希望将其与原始卡片进行比较的同等结果。目前我正在使用 (Integer/parseInt) 来转换字符串。
花色值可以从不同的类型(例如关键字)构建,但分值在其他地方用作数字。
In Clojure I am building a card game. Cards have a suit and a score.
{:suit 1 :score 9}
The cards are created using ranges, e.g. (range suitTotal), so the class of the values of :suit and :score is Long.
Players send command strings, e.g. "discard1.9" is a discard request.
Using a regex to parse this:
(re-seq #"[0-9]+" command)
results in String items "1" and "9". A card created with these results would be
{:suit "1" :score "9"}
I would like this to compare as equal with the original card. At the moment I am using (Integer/parseInt) to convert the strings.
The suit value could be built from a different type, such as a keyword, but the score value is used as a number elsewhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用读取字符串
DEMO
use read-string
DEMO
一个好的方法是将字符串解析为数字,然后使用 = 进行比较。
与读取字符串相比,它的优点是受到更多限制。这不会解析看起来像 clojure 数据结构的字符串。
A good approach would be to parse the strings as numbers and then use = to compare.
The advantage of this over read-string is this is more restricted. This won't parse strings that look like clojure data-structures.