Using map in clojure, checking whether a string contains uppercase character.
(map #(= (clojure.string/upper-case %) %) "Hello")
The expected result.
(true false false false false)
unfortunately, the result is unexpected.
(false false false false false)
I did an experiment when I replace "H" in the first "%", the result is still unexpected.
(map #(= (clojure.string/upper-case "H") %) "Hello")
(false false false false false)
When I replace "H" in the second "%", the result is changed, it is an expected result.
(map #(= (clojure.string/upper-case %) "H) "Hello")
(true false false false false)
What's wrong with that? Please feel free to comment.
As others have pointed out, comparing a character to a string will not work. Comparing strings will work:
(map #(= (clojure.string/upper-case %) (str %)) "Hello")
=> (true false false false false)
However this is more direct:
(map #(Character/isUpperCase %) "Hello")
=> (true false false false false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With