gpt4 book ai didi

clojure - 在 clojure 中,映射和字符串化 : make it simpler

转载 作者:太空宇宙 更新时间:2023-11-03 18:55:49 25 4
gpt4 key购买 nike

映射和字符串化

我有这段 clojure 代码:

(def up (memfn toUpperCase))   
(str "\n" (apply str (interpose "\n" (map up '("one" "two")))) "\n"); "\nONE\nTWO\n"

这段代码完全按照它应该做的去做:获取一个字符串列表,每个字符串都大写,然后用 \n(前后加上)将每个字符串括起来。

但必须有一种方法可以用更优雅的方式来编写它。请帮忙。

最佳答案

你可以合并 map 并插入:

(apply str "\n" (map #(str (up %) "\n") '("one" "two")))

另外,不一定更优雅,有点像 timtowdi 的精神:

(clojure.pprint/cl-format false "~%~{~:@(~A~)~%~}" '("one" "two"))

参见 practical common lisp有关 cl 格式字符串的教程。

关于clojure - 在 clojure 中,映射和字符串化 : make it simpler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9284630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com