Share your thoughts in the 2024 State of Clojure Survey!

Welcome! Please see the About page for a little more info on how this works.

0 votes
in Protocols by

I have a map structure that looks like this

 {:a "this"
  :b {:x "foo" :y 1}
  :c "that"
  :d {:x "bar" :y 2}
  :e "the other"}

That is, a top-level map that contains several keys, some of which have as values maps that conform to the same structure.

The values of :x and :y in that inner map structure can be applied to a function that would retrieve another map or datafyable object that I might navigate to. This led me to think of implementing the Navigable protocol so that I might use a REBL to browse through this data.

To do this I understand that I need to create a (nav coll k v) function and apply it as metadata to the map. My question is this:

Does the nav function belong on the top-level map or on each of the inner maps?

1 Answer

0 votes
by

You would add the metadata containing clojure.datafy/nav as the key (and your nav function as the value) to the collection that would be passed to nav, i.e., the nested maps.

It's worth thinking about the full context tho' (for REBL and other tools that use datafy and nav): you start out with "stuff" that is first datafy'd by those tools to produce "pure data" that can be navigated via get etc and also via nav.

So you go from "stuff" -> datafy -> "pure data" (plus nav metadata).

Then from "pure data" -> get -> "new pure data", or from "pure data" -> nav -> "new stuff corresponding to new pure data".

So you might want to start out with your map supporting datafy metadata so that when REBL etc datafies it, the result is navigable -- and could be augmented with additional keys and/or metadata as part of the datafication for display in REBL etc.

If you start with pure data (with nav metadata), calling datafy on it should be an identity function so you could just add nav metadata to the original map. I personally think it's better to start with datafy metadata (and have the implementation add the nav metadata) so that the stuff -> datafy -> data -> nav -> new-stuff pattern is more explicit. That's the approach I took in clojure.java.jdbc and next.jdbc -- and I blogged about datafy/nav in December 2018.

...