Skip to content

Instantly share code, notes, and snippets.

@jeremyheiler
Last active October 10, 2015 18:55
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 jeremyheiler/86dffef5b0fbe6a7eed3 to your computer and use it in GitHub Desktop.
Save jeremyheiler/86dffef5b0fbe6a7eed3 to your computer and use it in GitHub Desktop.
user=> (defprotocol P (plength [this]) (pget [this index]))
P
user=> (deftype Foo [v] P (plength [this] (count v)) (pget [this index] (nth v index)))
user.Foo
user=> (defn obj-seq [obj index] (lazy-seq (if-not (= index (plength obj)) (cons (pget obj index) (obj-seq obj (inc index))) nil)))
#'user/obj-seq
user=> (obj-seq (->Foo [1 2 3]))
ArityException Wrong number of args (1) passed to: user/obj-seq clojure.lang.AFn.throwArity (AFn.java:429)
user=> (obj-seq (->Foo [1 2 3]) 0)
(1 2 3)
user=> (let [[a b c] (obj-seq (->Foo [1 2 3]) 0)] (println a) (println b) (println c))
1
2
3
nil
user=> (defn obj-seq [obj index] (lazy-seq (if-not (= index (plength obj)) (cons (let [x (pget obj index)] (println x) x) (obj-seq obj (inc index))) nil)))
#'user/obj-seq
user=> (let [[a b c] (obj-seq (->Foo [1 2 3 4 5 6]) 0)] (str "got " a b c))
1
2
3
"got 123"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment