Thanks, I expected something more simple but this protocol works well. Before I stick to it, I'm just going to give a little bit more context.
I want to write a wrapper for the Java library Hector<https://github.com/rantav/hector>, client for Cassandra DB. And this tree structure would be useful in the API. Hector allows inserting many fields at once using the following syntax: mutator.addInsertion(”jsmith,” "Identification",HFactory.createStringColumn(“first”, “John”)) .addInsertion(“jsmith”, “Identification”, HFactory.createStringColumn(“last”, “Smith”)) .addInsertion(“jsmith”, “Identification”, HFactory.createStringColumn(“middle”, “Q”)) .execute(); To avoid the repetition of key ("jsmith") and column family (“Identification”), I thought a tree structure would be nice. Something like: (insert! {“jsmith” {“Identification” {“first” “John”, “last” “Smith”, “middle” “Q”}} {“Professional” {“occupation” “programmer”}}}) Does it make sense to use this kind of Map here, along with the protocol proposed by Meikel to flatten it and call the Hector API? Would I be better with vectors or something else? Thanks a lot 2011/2/21 Meikel Brandmeyer <m...@kotka.de> > Hi, > > On 21 Feb., 08:17, Damien <damienlep...@gmail.com> wrote: > > > Not sure if I should talk about flattening but basically I'm trying to > > achieve the following transformation: > > > > user=>(flatten-tree {1 {2 {3 4 5 6} 7 {8 9}}}) > > ((1 2 3 4) (1 2 5 6) (1 7 8 9)) > > Using protocols seems overkill: > > (defprotocol TreeFlattener (flatten-tree [this])) > > (extend-protocol TreeFlattener > clojure.lang.IPersistentMap > (flatten-tree > [this] > (mapcat (fn [[k v]] (map #(cons k %) (flatten-tree v))) this)) > Object > (flatten-tree > [this] > (list (list this))) > nil > (flatten-tree [_] (list (list nil)))) > > user=> (flatten-tree {1 {2 {3 [4 nil] 5 6} 7 {8 9}}}) > ((1 2 3 [4 nil]) (1 2 5 6) (1 7 8 9)) > > ... but it also allows to add other data structures later on: > > (extend-type clojure.lang.IPersistentVector > TreeFlattener > (flatten-tree > [this] > (mapcat (fn [idx x] (map #(cons idx %) (flatten-tree x))) (range) > this))) > > user=> (flatten-tree {1 {2 {3 [4 nil] 5 6} 7 {8 9}}}) > ((1 2 3 0 4) (1 2 3 1 nil) (1 2 5 6) (1 7 8 9)) > > Beware structure depths and stack overflows, though. > > Hope that helps. > > Sincerely > Meikel > > -- > 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 > -- Damien Lepage http://damienlepage.com -- 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