Skip to content

Instantly share code, notes, and snippets.

@juergenhoetzel
Created May 29, 2010 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juergenhoetzel/418216 to your computer and use it in GitHub Desktop.
Save juergenhoetzel/418216 to your computer and use it in GitHub Desktop.
(ns joc
(:use [clojure.test]))
(defn rpn-orig
([tokens] (rpn-orig tokens []))
([[top & tail] stack]
(lazy-seq
(if top
(if (fn? top)
(let [l (peek stack)
s (pop stack)
r (peek s)]
(rpn-orig tail (conj (pop s) (top r l))))
(rpn-orig tail (conj stack top)))
stack))))
(defn rpn
([[top & tail] & stack]
(lazy-seq
(if top
(if (fn? top)
(apply rpn tail (top (first stack) (second stack))
(drop 2 stack))
(apply rpn tail top stack))
stack))))
(deftest test-rpm
(are [x y] (= x y)
[3] (rpn [1 2 +])
[100000] (joc/rpn (vec (take 99999 (repeat +)))
(vec (take 100000 (repeat 1))))))
@juergenhoetzel
Copy link
Author

user> (time (apply joc/rpn (vec (take 99999 (repeat +)))
       (take 100000 (repeat 1))))

"Elapsed time: 52.788637 msecs"
(100000)

user> (time (joc/rpn-orig (vec (take 99999 (repeat +)))
          (vec (take 100000 (repeat 1)))))

"Elapsed time: 104.770117 msecs"
(100000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment