Stripping Vowels in clojure

clojure

Solution

You can take advantage of the underlying functions on the string class for that.

user=> (.replaceAll "hello world" "[aeiou]" "")           
"hll wrld"

If that feels like cheating, you could turn the string into a seq, and then filter it with the complement of a set, and then turn that back into a string.

user=> (apply str (filter (complement #{\a \e \i \o \u}) (seq "hello world")))
"hll wrld"

Sets in clojure are also functions. `complement` takes a function and returns a function that returns the logical not of the original function. It's equivalent to this. `apply` takes a function and a bunch of arguments and calls that function with those arguments (roughly speaking).

user=> (apply str (filter #(not (#{\a \e \i \o \u} %)) (seq "hello world")))
"hll wrld"

edit

One more...

user=> (apply str (re-seq #"[^aeiou]" "hello world"))
"hll wrld"

`#"[^aeiou]"` is a regex, and re-seq turns the matches into a seq. It's clojure-like and seems to perform well. I might try this one before dropping down to Java. The ones that seq strings are quite a bit slower.

Important Edit

There's one more way, and that is to use `clojure.string/replace`. This may be the best way given that it should work in either Clojure or Clojurescript.

e.g.

dev:cljs.user=> (require '[clojure.string :as str])
nil

dev:cljs.user=> (str/replace "hello world" #"[aeiou]" "")
"hll wrld"

Problem

I'm trying to write a function to strip all ASCII vowels in Clojure. I am new to Clojure, and I'm having a little trouble with strings. For example the string `"hello world"` would return `"hll wrld"`. I appreciate the help!

Original source