Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Chouser
Created September 24, 2010 16:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chouser/595663 to your computer and use it in GitHub Desktop.
Save Chouser/595663 to your computer and use it in GitHub Desktop.
(defprotocol Spinnable
(spin [this] "Return a seq walking the opposite direction as this"))
(defn iter-bi [x f b]
(reify
Spinnable
(spin [_] (iter-bi x b f))
clojure.lang.ISeq
(first [_] x)
(more [_] (iter-bi (f x) f b))
(next [this] (rest this)) ; same as rest since all iter-bi's are infinite seqs
(seq [this] this)
(equiv [_ _] false))) ; cheater!
(extend-type clojure.lang.LazySeq
Spinnable
(spin [this] (spin (seq this))))
(->> (iter-bi 0 inc dec)
(drop 4)
spin
(take 10))
;=> (5 4 3 2 1 0 -1 -2 -3 -4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment