In Python, I can do this:
>>> import string
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
Is there any way to do something similar in Clojure (apart from copying and pasting the above characters somewhere)? I looked through both the Clojure standard library and the java standard library and couldn't find it.
I'm pretty sure the letters aren't available in the standard library, so you're probably left with the manual approach.
No, because that is just printing out the ASCII letters rather than the full set. Of course, it's trivial to print out the 26 lower case and upper case letters using two for loops but the fact is that there are many more "letters" outside of the first 127 code points. Java's "isLetter" fn on Character will be true for these and many others.
Based on Michaels imperative Java solution, this is a idiomatic (lazy sequences) Clojure solution:
Update: Thanks cgrand for this preferable high-level solution:
But the performace comparison between my first approach
and your solution
is quite interesting.
In case you don't remember code points ranges. Brute force way :-P :
The same result as mentioned in your question would be given by the following statement that has to be manually generated in contrast to the Python solution:
I modified the answer from Michael Borgwardt. In my implementation there are two lists lowerCases and upperCases for two reasons:
string.letters is lowercases followed by uppercases.
Java Character.isLetter(char) is more than just uppercases and lowercases, so use of Character.isLetter(char) will return to much results under some charsets, for example "windows-1252"
From Api-Doc: Character.isLetter(char):
So if string.letters should only return lowercases and uppercases, the TITLECASE_LETTER, ,MODIFIER_LETTER and OTHER_LETTER chars have to be ignored.
Additionally: the behaviour of string.letters changes when changing the locale. This maybe won't apply to my solution, because changing the default locale does not change the default charset. From apiDoc:
I guess, the default charset cannot be changed within the started JVM. So the "change locale" behaviour of string.letters can not be realizied with just Locale.setDefault(Locale). But changing the default locale is anyway a bad idea: