Skip to content

Instantly share code, notes, and snippets.

@fogus
Forked from juergenhoetzel/gist:418216
Created June 1, 2010 13:10
Show Gist options
  • Save fogus/420927 to your computer and use it in GitHub Desktop.
Save fogus/420927 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))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment