Artificery

Menu

  • Home
  • Archives
  • Tags
  • About
  • Warrens
  • RSS
May 15, 2017

Interator Pattern

Intent

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

This is straight forward.

In clojure

In clojure collections:

  • lists
  • sets
  • hashmaps
  • vectors

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}))

Summery

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.


Tags: Software Clojure Behavrioral Design Pattern Java

« Memento Pattern Flyweight Pattern »

Copyright © 2024 Drew Verlee

Powered by Cryogen | Free Website Template by Download Website Templates