Clojure:生成所有键盘可输入字符 [英] Clojure: generate all keyboard typeable characters
问题描述
上下文
我想生成可以生成的所有字符:
- 打开笔记本电脑
- 按键盘上的单个键
- 按住shift键并按下键盘上的单个键
我目前有:
range(int \a)(int \z))
(range(int \A)(int \Z))
)))
然后手动添加更多字符,例如〜!@#$%^& _ + {} |:<>?,。/;'[] \
问题
有更优雅的方式吗?
编辑
是的,我指的是美国Qwerty键盘。
如果您查看 US ASCII图表,看起来你想要的所有字符都在(范围33 127)
。所以最简单的方法来获取序列
(map char(range 33 127))
但是如果你试图验证一个字符串只包含这些字符,请具有如下函数:
(defn valid-char? [c]
pre>
(let [i(int c)]
(and(> i 32)(< i 127))))
然后,您可以使用
每个?
验证字符串:user => (each?valid-char?hello world)
true
user => (每个?valid-char?hélloworld)
false
Context
I want to generate all characters that can be generated by:
- opening note pad
- pressing a single key on the keyboard
- holding shift + pressing a single key on the keyboard
What I currently have:
(concat (range (int \a) (int \z))
(range (int \A) (int \Z))
(range (int \0) (int \9)))
then manually appending more characters like ~!@#$%^&*()_+{}|:"<>?,./;'[]\
Question
Is there a more elegant way of doing this?
Edits
Yes, I'm referring to US Qwerty keyboard.
If you look at a US ASCII chart, it seems that all the characters you want are within (range 33 127)
. So the simplest way to get a sequence of all those characters is to convert that range to characters.
(map char (range 33 127))
But if you are trying to validate that a string contains only those characters, have a function like:
(defn valid-char? [c]
(let [i (int c)]
(and (> i 32) (< i 127))))
Then you can use it with every?
to validate a string:
user=> (every? valid-char? "hello world")
true
user=> (every? valid-char? "héllo world")
false
这篇关于Clojure:生成所有键盘可输入字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!