Skip to content

Instantly share code, notes, and snippets.

@hden
Created March 1, 2018 12:33
Show Gist options
  • Save hden/ad94ca8275133456abd80a1ee83b6d6b to your computer and use it in GitHub Desktop.
Save hden/ad94ca8275133456abd80a1ee83b6d6b to your computer and use it in GitHub Desktop.
(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)))
{: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