Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Solving the Roman Numeral Kata in Clojure
(ns roman-numerals.core-test
(:require [clojure.test :refer :all]))
(def mapping {
0 ""
1 "I"
4 "IV"
5 "V"
9 "IX"
10 "X"
(defn highest-reducer [x]
(let [x (->> mapping
(filter #(<= % x))
(apply max))]
[x (mapping x)]))
(defn convert
([x] (convert x ""))
([x result-string]
(let [[arabic roman] (highest-reducer x)]
(if (= x 0)
(recur (- x arabic)
(str result-string roman))))))
(deftest convert-test
(testing "converts arabic numbers to roman numerals"
(are [x y] (= (convert x) y)
1 "I"
2 "II"
4 "IV"
5 "V"
6 "VI"
7 "VII"
8 "VIII"
9 "IX"
10 "X"
11 "XI"
13 "XIII"
14 "XIV"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.