Skip to content

Instantly share code, notes, and snippets.

@jeaye
Created August 12, 2022 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeaye/c06c95b9e905ff317f7df1d9cf06bd64 to your computer and use it in GitHub Desktop.
Save jeaye/c06c95b9e905ff317f7df1d9cf06bd64 to your computer and use it in GitHub Desktop.
(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