在 Clojure 中,将用户输入字符串与长整型进行比较的好方法是什么?

发布于 2024-12-10 04:08:45 字数 436 浏览 0 评论 0原文

我正在用 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技术交流群

发布评论

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

评论(2

安人多梦 2024-12-17 04:08:45

使用读取字符串

DEMO

user=> (read-string "1")
1

use read-string

DEMO

user=> (read-string "1")
1
不忘初心 2024-12-17 04:08:45

一个好的方法是将字符串解析为数字,然后使用 = 进行比较。

user=> (Integer/parseInt "1")
1

与读取字符串相比,它的优点是受到更多限制。这不会解析看起来像 clojure 数据结构的字符串。

A good approach would be to parse the strings as numbers and then use = to compare.

user=> (Integer/parseInt "1")
1

The advantage of this over read-string is this is more restricted. This won't parse strings that look like clojure data-structures.

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