Hendekagon
Hendekagon

Reputation: 4643

How to use deftype to implement an associative structure in Clojurescript

I would like to provide a Clojurescript implementation of vector based on Javascript Typed Arrays, which supports assoc, replace etc. I think I want to do that (maybe there's a better way, or maybe someone's done it ?). Presumably I'd use deftype, but what protocols should I provide "concretions" for ?

Upvotes: 4

Views: 515

Answers (1)

levand
levand

Reputation: 8500

Let's see what the built-in vectors implement. You can view the source here, on github.

Looks like it's: Object, IWithMeta, IMeta, IStack, ICollection, IEmptyableCollection, ISequential, IEquiv, IHash, ISeqable, ICounted, IIndexed, ILookup, IMapEntry, IAssociative, IVector, IReduce, IKVReduce, IFn, IEditableCollection and IReversible.

That's a lot, but since each one defines one or at most two methods, it isn't that much work. Plus, you could leave some of them unimplemented, like IEditableCollection which is only used for transients or IReduce which is for the new reducers functions.

You don't even have to make your new data structure work exactly like a built-in Vector, either. You could have it implement all the sequential stuff and not worry about the map stuff, for example, though of course then it would be less convenient than a normal Vector.

Upvotes: 3

Related Questions