On 8/1/13 5:59 PM, JvJ wrote:
> I'm looking for an associative data structure that can be accessed by both
> rows and columns, and could potentially be sparse.
>
> Suppose the following table is called t:
>
> | | :A | :B | :C ||---+------+------+----------|| 1 | |
> | '[x y z] || 2 | "2a" | "2b" | || 3 | | | || 3 |
> :3a | | "Foo" |
>
>
> Then (t :A) would return {2 "2a", 3 :3a}, and (t 2) would return {:A "2a",
> :B "2b"}.
> (t :A 2) or (t 2 :A) would return "2a".
>
> I'm thinking of implementing it as a simple map of maps with some extra
> functions, but I'm not sure if
> that would be the best option.
>
>
I would recommend looking at the clojure.set namespace. it has an index
function which builds indices
user> (require '[clojure.set :as s])
nil
user> (clojure.repl/doc s/index)
-------------------------
clojure.set/index
([xrel ks])
Returns a map of the distinct values of ks in the xrel mapped to a
set of the maps in xrel with the corresponding values of ks.
nil
user> (s/index [{:row 1 :col 2}] [:row])
{{:row 1} #{{:row 1, :col 2}}}
user> (merge (s/index [{:row 1 :col 2}] [:row]) (s/index [{:row 1 :col
2}] [:col]) (s/index [{:row 1 :col 2}] [:row :col]))
{{:col 2, :row 1} #{{:row 1, :col 2}}, {:col 2} #{{:row 1, :col 2}},
{:row 1} #{{:row 1, :col 2}}}
user> (def d (merge (s/index [{:row 1 :col 2}] [:row]) (s/index [{:row 1
:col 2}] [:col]) (s/index [{:row 1 :col 2}] [:row :col])))
#'user/d
user> (get d {:col 2})
#{{:row 1, :col 2}}
user> (get d {:row 1})
#{{:row 1, :col 2}}
user> (get d {:row 1 :col 2})
#{{:row 1, :col 2}}
user>
--
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?
signature.asc
Description: OpenPGP digital signature
