-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path121-universal-computation-engine.clj
21 lines (21 loc) · 1.15 KB
/
121-universal-computation-engine.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(fn [form]
(letfn [(step [s]
(let [formulars (re-seq #"\([^\(\)]*\)" s)]
(if (seq formulars)
(let [formulars-results (map
(fn [formular]
(let [formular-seq (re-seq #"[\+\-\*/0-9]+" formular)]
(apply
(case (first formular-seq)
"+" +
"-" -
"*" *
"/" /)
(map #(Integer/parseInt %) (rest formular-seq)))
))
formulars)]
(step (reduce #(.replace %1 (key %2) (str (val %2))) s (apply assoc {} (interleave formulars formulars-results)))))
(Integer/parseInt s))))]
(fn [m]
(let [form-str (str form)]
(step (reduce #(.replace %1 (str (key %2)) (str (val %2))) form-str m))))))