Skip to content

Instantly share code, notes, and snippets.

@harmenjanssen
Created May 5, 2020 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harmenjanssen/3fbdeff221777f43697c389b0f1e4d21 to your computer and use it in GitHub Desktop.
Save harmenjanssen/3fbdeff221777f43697c389b0f1e4d21 to your computer and use it in GitHub Desktop.
nearest-vowels Clojure kata
(def vowels "aeiou")
(defn is-vowel [ch] (not (nil? (some #{ch} vowels))))
(defn abs [n] (max n (- n)))
(defn distance
"Returns the distance from the number x to the closest number in xs"
[xs x]
(apply min (map (fn [y] (abs (- y x))) xs)))
(defn nearest-vowels [string]
(let [vowel-indices (->> string
(map-indexed vector)
(filter #(is-vowel (second %)))
(map first))]
(if (not-empty vowel-indices)
(map-indexed (fn [index ch] (distance vowel-indices index)) string)
(map #{nil} string))))
(nearest-vowels "babbba") ;; (1 0 1 2 1 0)
(nearest-vowels "hjkl") ;; (nil nil nil nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment