Created
January 9, 2017 02:57
-
-
Save sebastibe/0422b911e3c91022d01a5bf2a40a7026 to your computer and use it in GitHub Desktop.
We want to know the index of the vowels in a given word, for example, there are two vowels in the word 'super' (the second and fourth letters).
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
(def vowels #{\a \A \e \E \i \I \o \O \u \U \y \Y}) | |
;; First approach | |
(defn vowel-indices [word] | |
(let [matches (map vowels (seq word))] | |
(into [] (remove nil? (map-indexed (fn [idx el] (if-not (nil? el) (+ idx 1))) matches))))) | |
;; Better approach | |
(defn vowel-indices [word] | |
(keep-indexed #(when (vowels %2) (inc %1)) word)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment