As a newbie to Clojure, I am sure all of us will meet one problem when dealing with string. Let’s say we have a long string.
What if we want to remove all spaces ? Let’s say we’ll solve this by filter through the text and apply a function to check if it’s a space. That’s simple.
1 2 3
Array.prototype.filter is a method for array, so firstly we turn string into array by using
split method. After filtering through it we will join it back.
What if we try to do this in Clojure:
Let’s see why this doesn’t work in Clojure and find a solution for it. In Clojure, strings are sequences of characters. And Clojure characters are Java characters:
1 2 3 4
Let’s concat some characters using the
As I said, strings are sequences of characters. So when we call sequences function in Clojure on strings, we will get a sequence(consistency!). Let’s first filter through the string and remove the space.
We can see at first strings are turned into sequences and get mapped to apply our function
#(not= % \space). Indeed we can see what does it looks like:
Since we know how Clojure
str works, it’s very tempting for us to do:
The problem is this won’t work since we are passing a sequence of characters to the function
str instead of strings or characters.
The possible solution to this is using the
apply function. Let’s see how
apply unroll the parameter
seq and apply function
f to them (Note:
apply can accept one more optional parameter.). Seems like we can solve our problem now:
Another solution is to use
clojure.string/join which is a wrapper for
(apply str args).
Clojure strings are sequences of characters and Clojure characters are Java characters. When a sequence function is use on string, we have to be careful on the result which is probably a list. To deal with that, we can always use the great
apply or it’s wrapper for
(apply str ...) –