Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.