Last active
December 31, 2015 19:29
-
-
Save greduan/8034054 to your computer and use it in GitHub Desktop.
Attempt at the string calculator kata in Clojure http://nimblepros.com/media/36628/string%20calculator%20kata.pdf
This file contains 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
(ns kata-1.core) | |
(defn- str->nums [s] | |
(map read-string (re-seq #"[\d]+|[-\d]+" s))) | |
(defn add [s] | |
(let [nums (str->nums s)] | |
(when-let [neg-nums (not-empty (filter neg? nums))] | |
(throw (Exception. (str "Negatives not allowed:" neg-nums)))) | |
(reduce + (filter #(<= % 1000) nums)))) |
This file contains 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
(ns kata-1.core-test | |
(:require [kata-1.core :refer :all]) | |
(:use expectations)) | |
;; step 1 | |
(expect 0 (add "")) | |
(expect 1 (add "1")) | |
(expect 3 (add "1,2")) | |
;; step 2 | |
(expect 6 (add "1,1,1,2,1")) | |
;; step 3 | |
(expect 6 (add "1\n2,3")) | |
;; step 4 | |
(expect 3 (add "//;\n1;2")) | |
;; step 5 | |
(expect Exception (add "-1,2")) | |
(expect Exception (add "2,-4,3,-5")) | |
;; step 6 | |
(expect 2 (add "1001,2")) | |
;; step 7 | |
(expect 6 (add "//[****]\n1***2***3")) | |
;; step 8 | |
(expect 6 (add "//[*][%]\n1*2%3")) | |
;; step 9 | |
(expect 6 (add "//[*][%]\n1**2%%%3")) |
This file contains 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
(defproject kata-1 "0.1.0-SNAPSHOT" | |
:description "FIXME: write description" | |
:url "http://example.com/FIXME" | |
:license {:name "Eclipse Public License" | |
:url "http://www.eclipse.org/legal/epl-v10.html"} | |
:dependencies [[org.clojure/clojure "1.5.1"] | |
[expectations "1.4.52"]] | |
:plugins [[lein-autoexpect "1.0"]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment