And ~44ms on mine. Impressive! Will study your code tomorrow, thanks.

On Tuesday, July 24, 2012 12:07:59 AM UTC+3, David Nolen wrote:
>
> (deftype Tree [n l r] 
>   clojure.lang.Indexed 
>   (nth [this idx] 
>     (nth this idx nil)) 
>   (nth [_ idx not-found] 
>     (case idx 
>       0 n 
>       1 l 
>       2 r 
>       not-found))) 
>
> (defn generate-tree [[h & t :as coll]] 
>   (when (seq coll) 
>     (let [lr (generate-tree t)] (Tree. h lr lr)))) 
>
> (defn to-list 
>   ([node] (persistent! (to-list node (transient [])))) 
>   ([[v l r :as node] acc] 
>      (if-not (nil? node) 
>        (let [lacc (to-list l acc) 
>              _   (conj! lacc v) 
>              racc (to-list r lacc)] 
>          racc) 
>        acc))) 
>
> (defn tree [n] 
>   (generate-tree (range 1 n))) 
>
> (let [t (tree 21)] 
>    (dotimes [_ 5] 
>      (time 
>       (dotimes [_ 1] 
>         (count (to-list t)))))) 
>
> Takes ~53ms on my machine. 
>
> David
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to