Created
May 29, 2010 11:09
-
-
Save juergenhoetzel/418216 to your computer and use it in GitHub Desktop.
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 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)))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"Elapsed time: 52.788637 msecs"
(100000)
"Elapsed time: 104.770117 msecs"
(100000)