I'm trying to write a function that takes a string and returns a result of a filter function (I'm working through 4clojure problems). The result must be a string too.
I've written this:
(fn my-caps [s]
(filter #(Character/isUpperCase %) s))
(my-caps "HeLlO, WoRlD!")
Result: (\H \L \O \W \R \D)
Now I'm trying to create a string out of this list, using clojure.string/join, like this:
(fn my-caps [s]
(clojure.string/join (filter #(Character/isUpperCase %) s)))
The result is however the same. I've also tried using apply str
, with no success.
You have to convert the lazy sequence returned by filter
into a string, by applying the str
function. Also, use defn
to define a new function - here's how:
(defn my-caps [s]
(apply str (filter #(Character/isUpperCase %) s)))
It works as expected:
(my-caps "HeLlO, WoRlD!")
=> "HLOWRD"
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