开发者

In Clojure, what is a good way to compare user input Strings with Longs?

开发者 https://www.devze.com 2023-04-13 02:56 出处:网络
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

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 keywor开发者_如何转开发d, but the score value is used as a number elsewhere.


use read-string

DEMO

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


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.

0

精彩评论

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