map in clojure giving unexpected result

1

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.

Share
Improve this question
4
  • You said you need to check whether a string contains an uppercased character so am I right in assuming you don't need to know which one? In this case why not checking the string as whole? (= (str/lower-case "Hello") "Hello") should tell you whether your string was lowercased initially – customcommander Apr 23 at 10:02
  • It is a small part of program, for some reasons, I need to check a long string with one character by character. – madeinQuant Apr 23 at 10:06
  • 2
    You compare string with a char, your test actually looks like this: (= "H" \H) which is false. – generateme Apr 23 at 10:12
  • There are some good answers below. You may also be interested in the helper & convenience functions in tupelo.chars (cljdoc.org/d/tupelo/tupelo/21.04.13/api/tupelo.chars) and tupelo.string (cljdoc.org/d/tupelo/tupelo/21.04.13/api/tupelo.string) – Alan Thompson Apr 23 at 15:16

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue