Welcome! Please see the About page for a little more info on how this works.

0 votes
in Syntax and reader by

(= (map :name (mapify (parse (slurp filename)))) '("Edward Cullen" "Bella Swan" "Charlie Swan" "Jacob Black" "Carlisle Cullen" "Joni balap"))
=> true

(clojure.string/includes? (map :name (mapify (parse (slurp filename)))) "Joni")
=> false

(clojure.string/includes? '("Edward Cullen" "Bella Swan" "Charlie Swan" "Jacob Black" "Carlisle Cullen" "Joni balap") "Joni")
=> true

1 Answer

+3 votes
by
selected by
 
Best answer

Let's take a look at what clojure.string/includes? says it does and how it is implemented:

user=> (doc clojure.string/includes?)
-------------------------
clojure.string/includes?
([s substr])
  True if s includes substr.
nil
user=> (source clojure.string/includes?)
(defn includes?
  "True if s includes substr."
  {:added "1.8"}
  [^CharSequence s ^CharSequence substr]
  (.contains (.toString s) substr))
nil
user=> (.toString (map :name [{:name "One"} {:name "Two"}]))
"clojure.lang.LazySeq@26e067"
user=> (.toString ["One" "Two"])
"[\"One\" \"Two\"]"
user=>

We see that it assumes its first argument is a string -- or more specifically a CharSequence -- and it starts out by calling .toString on that argument.

We see that (.toString (map .. ..)) produces a cryptic-looking string but this is because map produces a lazy sequence and the default string representation of a lazy sequence is its type and a hex value, like any Java object that doesn't have a specific string representation.

However, calling .toString on a quoted list or a vector produces a string representation of the elements of the list or vector.

Consequently, clojure.string/includes? "works" in the latter case because even though you are not passing it a CharSequence, when it converts the data structure to a string, it includes the name you are looking for.

Since you want to check if any element of the sequence contains the string, what you probably want here is:

(some #(clojure.string/includes? % "Joni") (map :name (mapify ..)))
by
Ahhh I see....
Thank you very much for answering my question, That's helps me a lot!
by
And to follow up on this - the toString is there to turn the CharSequence into a String. While it does have the side effect of stringifying lots of other things, that should be considered undefined behavior and you should convert the input to a string before calling any function in clojure.string.
...