Last active
December 19, 2017 21:47
-
-
Save ultimapanzer/6ce500d0361ccdc08c8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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 |
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
You can also use the regex engine:
This will likely be the fastest for large inputs