Last active
October 10, 2015 18:55
-
-
Save jeremyheiler/86dffef5b0fbe6a7eed3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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