Skip to content

Instantly share code, notes, and snippets.

@ultimapanzer
Last active December 19, 2017 21:47
Show Gist options
  • Save ultimapanzer/6ce500d0361ccdc08c8e to your computer and use it in GitHub Desktop.
Save ultimapanzer/6ce500d0361ccdc08c8e to your computer and use it in GitHub Desktop.
;; Try it in the REPL or eval in Light Table
(require '[clojure.string :as string])
(def vowels [:a :e :i :o :u])
(defn char-is-consonant?
[char]
(not-any? #{(keyword char)} vowels))
(def char-is-vowel? (complement char-is-consonant?))
(defn string-to-vec
"Converts a string to a vector of strings"
[string]
(vec (drop 1 (string/split string #""))))
(defn count-vowels
"Returns the number of vowels in a word"
[word]
(count (filter char-is-vowel? (string-to-vec word))))
(count-vowels "foobarbazbuzz")
;;-> 5
@guns
Copy link

guns commented May 20, 2014

You can also use the regex engine:

(defn count-ascii-vowels [s]
  (count (re-seq #"(?i)[aeiou]" s)))

This will likely be the fastest for large inputs

@ultimapanzer
Copy link
Author

Cool, I didn't realize that strings were directly filterable, and using a set for the filter is nice too. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment