Search code examples
clojurecharactersequence

Generate character sequence from 'a' to 'z' in clojure


I want to generate character sequence from 'a' to 'z'. In scala, I can generate character sequence very simply:

('a' to 'z')

But in clojure, I end up with the following code:

(->> (range (int \a) (inc (int \z))) (map char))

or

(map char (range (int \a) (inc (int \z))))

It seems to me verbose. Are there any better ways to do it?


Solution

  • 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)