Part of what’s so powerful about Clojure is that all the core data-types implement the same sequence abstraction: clojure.lang.ISeq.
This means that functions like “first”, “concat”, “cons”, “map”, “rest”, etc work generically on all of those data-types.
My question is this: how can I add my own custom function into the mix, and have it work for all the types that extend from ISeq?
One first attempt was to define my own protocol, then “(extend-type clojure.lang.ISeq …”, but that doesn’t work (it compiles but doesn’t add the behavior to the actual types). Another idea was to write a macro that does an “extend-type” explicitly on all the Clojure types (PersistentHashMap, PersistentList, etc), but that seems kludgey.
Is there any elegant/idiomatic way to do this? Multimethods perhaps?
What exactly are you trying to do?
If you’re trying to add behaviour to existing types: either write normal functions that deal with seqs or use multimethods or
extendto do what you want.Also, something to note is that most Clojure “sequence” types (vectors, sets, maps) are not in themselves sequences (they do not implement
clojure.lang.ISeq), so you have to do more than just add toclojure.lang.ISeqif you wish to support them.