Skip to content

Instantly share code, notes, and snippets.

@sebastibe
Created January 9, 2017 02:57
Show Gist options
  • Save sebastibe/0422b911e3c91022d01a5bf2a40a7026 to your computer and use it in GitHub Desktop.
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).
(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