-
-
Save jeaye/c06c95b9e905ff317f7df1d9cf06bd64 to your computer and use it in GitHub Desktop.
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 arithmetic.core | |
(:require [clojure.string :as string])) | |
(defn tokenize [expr] | |
(string/split expr #"\s+")) | |
(defn -main [& [expr]] | |
(let [tokens (tokenize expr) | |
reduction (reduce (fn [acc token] | |
(case token | |
"+" (assoc acc :op +) | |
"-" (assoc acc :op -) | |
(if-some [op (:op acc)] | |
(-> (update acc :result op (read-string token)) | |
(assoc :op nil)) | |
(assoc acc :result (read-string token))))) | |
{:result 0 | |
:op nil} | |
tokens)] | |
(println (:result reduction)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment