Created
March 1, 2018 12:33
-
-
Save hden/ad94ca8275133456abd80a1ee83b6d6b to your computer and use it in GitHub Desktop.
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
(ns baseball | |
(:require [clojure.core.match :refer [match]])) | |
(def initial-state {:ball 0 | |
:strike 0 | |
:out 0 | |
:error 0}) | |
(defn rules [state] | |
(match [state] | |
;; BB | |
[{:ball 4}] {:ball 0 :strike 0} | |
;; 3 Outs | |
[{:strike 3 :out 2}] initial-state | |
;; Out | |
[{:strike 3}] {:ball 0 :strike 0 :out (inc (:out state))} | |
:else state)) | |
(defn key-for [x] | |
(condp = x | |
"B" :ball | |
"S" :strike | |
:error)) | |
(defn calculate-score [s] | |
(reduce (fn [state batting] | |
(->> (update state (key-for batting) inc) | |
rules | |
(merge state))) | |
initial-state | |
(map str s))) |
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
{:deps | |
{org.clojure/core.match {:mvn/version "0.3.0-alpha5"}} | |
:aliases | |
{:repl {:extra-deps {com.bhauman/rebel-readline {:mvn/version "0.1.1"}} | |
:main-opts ["-m" "rebel-readline.main"]}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment