Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure join fails to create a string from the result of filter function

Tags:

clojure

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.

like image 466
Anna Pawlicka Avatar asked Jun 23 '13 22:06

Anna Pawlicka


1 Answers

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"
like image 186
Óscar López Avatar answered Sep 25 '22 13:09

Óscar López