在 Clojure 中从字符串创建列表

发布于 2024-10-15 01:32:35 字数 450 浏览 2 评论 0原文

我正在寻找使用字符串作为源来创建字符列表。我做了一些谷歌搜索,但一无所获,所以我写了一个函数来完成我想要的功能:

(defn list-from-string [char-string]
  (loop [source char-string result ()]
    (def result-char (string/take 1 source))
    (cond
     (empty? source) result
     :else (recur (string/drop 1 source) (conj result result-char)))))

但是看到这个让我觉得我一定错过了一个技巧。

  1. 是否有核心或贡献功能可以为我做到这一点?当然我只是愚蠢吧?
  2. 如果没有,有没有办法改进这段代码?
  3. 同样的事情也适用于数字吗?

I'm looking to create a list of characters using a string as my source. I did a bit of googling and came up with nothing so then I wrote a function that did what I wanted:

(defn list-from-string [char-string]
  (loop [source char-string result ()]
    (def result-char (string/take 1 source))
    (cond
     (empty? source) result
     :else (recur (string/drop 1 source) (conj result result-char)))))

But looking at this makes me feel like I must be missing a trick.

  1. Is there a core or contrib function that does this for me? Surely I'm just being dumb right?
  2. If not is there a way to improve this code?
  3. Would the same thing work for numbers too?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(4

鸠书 2024-10-22 01:32:36

编辑:哎呀,我以为你想要一个不同字符的列表。为此,请使用核心功能“频率”。

clojure.core/frequencies
([coll])
  Returns a map from distinct items in coll to the number of times they appear.

示例:

user=> (frequencies "lazybrownfox")
{\a 1, \b 1, \f 1, \l 1, \n 1, \o 2, \r 1, \w 1, \x 1, \y 1, \z 1}

那么您所要做的就是获取密钥并将它们转换为字符串(或不转换为字符串)。

user=> (apply str (keys (frequencies "lazybrownfox")))
"abflnorwxyz"

Edit: Oops, thought you wanted a list of different characters. For that, use the core function "frequencies".

clojure.core/frequencies
([coll])
  Returns a map from distinct items in coll to the number of times they appear.

Example:

user=> (frequencies "lazybrownfox")
{\a 1, \b 1, \f 1, \l 1, \n 1, \o 2, \r 1, \w 1, \x 1, \y 1, \z 1}

Then all you have to do is get the keys and turn them into a string (or not).

user=> (apply str (keys (frequencies "lazybrownfox")))
"abflnorwxyz"
痕至 2024-10-22 01:32:36
(apply str (set "lazybrownfox")) => "abflnorwxyz"
(apply str (set "lazybrownfox")) => "abflnorwxyz"
悲念泪 2024-10-22 01:32:35

您可以使用 seq 函数来执行此操作:

user=> (seq "aaa")
(\a \a \a)

对于数字,您可以使用“哑”解决方案,例如:

user=> (map (fn [^Character c] (Character/digit c 10)) (str 12345))
(1 2 3 4 5)

clojure 中的 PS 字符串是“seq”的,因此您可以将它们用作任何序列的源处理函数 - map、for、...

You can just use seq function to do this:

user=> (seq "aaa")
(\a \a \a)

for numbers you can use "dumb" solution, something like:

user=> (map (fn [^Character c] (Character/digit c 10)) (str 12345))
(1 2 3 4 5)

P.S. strings in clojure are 'seq'able, so you can use them as source for any sequence processing functions - map, for, ...

烟燃烟灭 2024-10-22 01:32:35

如果您知道输入将是字母,只需用于

user=> (seq "abc")
(\a \b \c)

数字,请尝试此

user=> (map #(Character/getNumericValue %) "123")
(1 2 3)

if you know the input will be letters, just use

user=> (seq "abc")
(\a \b \c)

for numbers, try this

user=> (map #(Character/getNumericValue %) "123")
(1 2 3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文