Extending the core Clojure protocols
Warning: I'm pretty sure I'm misusing at least some of the relevant terms
I want to modify flatland.ordered.set.OrderedSet to nth
work. I think this includes something like:
(extend-type flatland.ordered.set.OrderedSet
?????
(nth [this n] (nth (vec this) n))
I am trying to figure out which protocol is determining nth
within hours, with no luck. Is there a list of native protocols? Am I just messed up?
source to share
It is currently not possible to do what you want to do using extend-type
. Clojure persistent data collection interfaces are implemented using Java interfaces, not Clojure. Therefore, they cannot be expanded with extend-type
.
However, since the code is open source, you can always change the library itself. All you have to do is implement nth
in OrderedSet
deftype
. nth
defined by the interface clojure.lang.Indexed
.
source to share
As Nathan Davis says, you can't do it "from the outside" because this stuff is based on interfaces, not protocols. It would be wise for OrderedSet to implement Indexed; I must have just ignored this interface.
On the other hand, your nth implementation is very inefficient: you don't want to create a vector of length N just to search for a single element. Instead, you want to invoke get
, which does exactly the same thing as nth
.
Edit : Looking at the code again, I see that it is nth
not so easy to implement correctly, because the existence of disj makes it difficult to quickly determine the number of items was dropped from the set where. I don't think nth
there can actually be an efficient implementation for this data structure unless you remove the use case disj
. So I probably won't accept a pull request implementing nth
unless you find something really smart, but feel free to fork ordered
and add it to your own fork if you don't need support disj
.
source to share