在clojure中从“a”到“z”生成字符序列 [英] Generate character sequence from 'a' to 'z' in clojure

查看:134
本文介绍了在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 \b \c \d \e \f)

这篇关于在clojure中从“a”到“z”生成字符序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆