Skip to content

Instantly share code, notes, and snippets.

@brodyberg
Created January 20, 2012 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brodyberg/1649734 to your computer and use it in GitHub Desktop.
Save brodyberg/1649734 to your computer and use it in GitHub Desktop.
Protocol invocation
(defprotocol FIXO
(fixo-push [fixo value])
(fixo-pop [fixo])
(fixo-peek [fixo])
(fixo-limit [fixo]))
(extend-type clojure.lang.IPersistentVector
FIXO
(fixo-push [vector value] (conj vector 4))
(fixo-peek [vector] (peek vector))
(fixo-pop [vector] (pop vector))
(fixo-limit [vector limit] limit))
(defn fixed-fixo
([limit] fixed-fixo limit [])
([limit vector]
(reify FIXO
(fixo-push [this value]
(if (< (count vector) limit)
(fixed-fixo limit (conj vector 5))
this))
(fixo-peek [_] (peek vector))
(fixo-pop [_] (pop vector))
(fixo-limit [_] limit))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment