On Fri, Nov 13, 2009 at 6:48 PM, ajuc <aju...@gmail.com> wrote:

> Hello.
>
> I've tried to translate nice Hilbert-curve-index calculating function
> to clojure (http://blog.notdot.net/2009/11/Damn-Cool-Algorithms-
> Spatial-indexing-with-Quadtrees-and-Hilbert-Curves).
>
> I've got sth like that:
>
> (def hilbert-map {
>  :a {[0 0] [0 :d], [0 1] [1 :a], [1 0] [3 :b], [1 1] [2 :a]}
>  :b {[0 0] [2 :b], [0 1] [1 :b], [1 0] [3 :a], [1 1] [0 :c]}
>  :c {[0 0] [2 :c], [0 1] [3 :d], [1 0] [1 :c], [1 1] [0 :b]}
>  :d {[0 0] [0 :a], [0 1] [3 :c], [1 0] [1 :d], [1 1] [2 :d]} } )
>
> (defn point-to-hilbert
>  "Return index of point (x,y) on Hilbert curve defined on square
> (2^order)*(2^order).
>    x,y should be in 0 .. (2^order)-1
>    result will be in 0 .. (2^(order*2))-1"
>  [x y order]
>  (let [step-inside (fn [x i] (if (== 0 (bit-and x (bit-shift-left 1
> i))) 0 1))
>         step (fn [square i] ((hilbert-map square) [(step-inside x i)
> (step-inside y i)]))]
>    (loop [ square :a, position 0, i (dec order)]
>      (if (< i (int 0))
>        position
>        (let [ v (step square i)]
>          (recur (v 1), (-> position (bit-shift-left 2) (bit-or (v
> 0))), (dec i)))))))
>
> (time (dotimes [ _ (int 1000)] (point-to-hilbert (rand-int (int
> -1000)) (int 1) (int 16))))
>
>
> I would like to somehow hide the global hilbert-map into my function,
> but I can't see how to do that.
>

Just put the literal directly into the function.


> Is this possible? I know that I can just inert literal into my let,
> but that degrades performance, when function is called many times.


Have you tested it, using (time ...), preferably on the server VM? I
wouldn't be surprised if a calculation that produces a constant gets
optimized away to just the constant.

-- 
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