Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
This is straight forward.
In clojure collections:
Implement the sequences interface.
Seqs differ from iterators in that they are persistent and immutable, not stateful cursors into a collection.
So you can call first on any collection..
(first [1 2])
(first #{1 2})
(first {:a 1 :b 2})
(first '(1 2))
or rest:
(rest [1 2])
(rest {:a 1 :b 2})
(rest '(1 2))
(rest #{1 2})
Notice that what we get back from (rest #{1 2})
is actual a seq, not a vector #{}
.
(seq? (rest #{1 2}))
Sequences are a deep fundamental topic in Clojure, i suggesting reading more about here In clojure we rarely define new data structures, but if you did, then it would probably be worth considering haveing them implement the Sequence interface.