Skip to content

Instantly share code, notes, and snippets.

@vaer-k
Last active February 14, 2018 03:32
Show Gist options
  • Save vaer-k/fd3ec8b41763fe61e347fe91afe7fe91 to your computer and use it in GitHub Desktop.
Save vaer-k/fd3ec8b41763fe61e347fe91afe7fe91 to your computer and use it in GitHub Desktop.
polish calulator
(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