本文介绍了在clojure中生成从'a'到'z'的字符序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想生成从a"到z"的字符序列.在scala中,我可以非常简单地生成字符序列:
I want to generate character sequence from 'a' to 'z'.In scala, I can generate character sequence very simply:
('a' to 'z')
但是在 clojure 中,我最终得到了以下代码:
But in clojure, I end up with the following code:
(->> (range (int a) (inc (int z))) (map char))
或
(map char (range (int a) (inc (int z))))
在我看来,这很冗长.有什么更好的方法吗?
It seems to me verbose. Are there any better ways to do it?
推荐答案
如果代码看起来冗长",通常只是表明您应该将其分解为一个单独的函数.作为奖励,您有机会为函数指定一个有意义的名称.
If code looks "verbose" it's often just a sign that you should factor it out into a separate function. As a bonus you get the chance to give the function a meaningful name.
只要做这样的事情,你的代码就会更具可读性:
Just do something like this and your code will be much more readable:
(defn char-range [start end]
(map char (range (int start) (inc (int end)))))
(char-range a f)
=> (a c d e f)
这篇关于在clojure中生成从'a'到'z'的字符序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!