I think you've reinvented hashmaps:

{12 "a" 23 "aa" 25 "aaa" 234 "aaaa"}

On May 25, 6:51 pm, Andreas Kostler <andreas.koestler.le...@gmail.com>
wrote:
> Hi all,
> has anyone spent some thought on how to efficiently represent sparse vectors 
> in Clojure?
> A naive scheme I came up with is using a vector of [idx val] pairs, e.g.:
> (def sparse-vec [[12 "a"][23 "aa"][25 "aaa"][234 "aaaa"]])
>
> Accessing a value at an idx can be done so:
> (get-nth sparse-vec 25)
> => "aaa"
>
> with:
>
> (defn get-nth [sparse-vec n]
>         (let [indices (map (fn [[i _]] i) sparse-vec)
>              idx (java.util.Collections/binarySearch indices n)
>              [_ v] (nth sparse-vec idx)]
>         v))
>
> For this to work the indices have to be in order. Can anyone think of more 
> efficient (algorithmically and implementation wise)
> ways of doing this?
>
> Kind Regards
> Andreas

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