Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

map in clojure giving unexpected result

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.

like image 453
madeinQuant Avatar asked Apr 06 '26 19:04

madeinQuant


1 Answers

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)
like image 119
Steffan Westcott Avatar answered Apr 16 '26 17:04

Steffan Westcott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!