Last active
February 14, 2018 03:32
-
-
Save vaer-k/fd3ec8b41763fe61e347fe91afe7fe91 to your computer and use it in GitHub Desktop.
polish calulator
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 clojure.reverse-notation | |
| (:require [clojure.edn :as edn])) | |
| (def operators {'+ + '- - '/ / '* *}) | |
| (defn calc | |
| [expr] | |
| (loop [[e & es] (->> expr (re-seq #"\S+") (map edn/read-string) (replace operators)) | |
| stack '()] | |
| (cond | |
| (number? e) (recur es (conj stack e)) | |
| (fn? e) (recur es (->> stack (take 2) reverse (apply e) (conj (drop 2 stack)))) | |
| :default (or (peek stack) 0)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment