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

Viewed 7666

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?

4 Answers

If you're interested in a library that gives you a convenient char-range function, my library djy has one: see djy.char/char-range.

boot.user=> (char-range \a \z)
(\a \b \c \d \e \f \g \h \i \j \k \l \m \n \o \p \q \r \s \t \u \v \w \x \y \z)

It even handles supplemental Unicode characters that are large enough that they require 2 characters, representing them as strings:

boot.user=> (char-range (char' 0x1f910) (char' 0x1f917))
("" "" "" "" "" "" "" "")

djy: a library of character utility functions for Clojure

Related