Created
September 7, 2011 21:35
-
-
Save m0smith/1201824 to your computer and use it in GitHub Desktop.
Updated map-indexed to add start and step to the indexing
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
(defn map-indexed | |
"Returns a lazy sequence consisting of the result of applying f to 0 | |
and the first item of coll, followed by applying f to 1 and the second | |
item in coll, etc, until coll is exhausted. Thus function f should | |
accept 2 arguments, index and item." | |
{:added "1.2"} | |
([f coll] (map-indexed f 0 1 coll)) | |
([f start coll] (map-indexed f start 1 coll)) | |
([f start step coll] | |
(letfn [(mapi [idx coll] | |
(lazy-seq | |
(when-let [s (seq coll)] | |
(if (chunked-seq? s) | |
(let [c (chunk-first s) | |
size (int (count c)) | |
b (chunk-buffer size)] | |
(dotimes [i size] | |
(chunk-append b (f (+ idx (* i step)) (.nth c i)))) | |
(chunk-cons (chunk b) (mapi (+ idx (* size step)) (chunk-rest s)))) | |
(cons (f idx (first s)) (mapi (+ step idx) (rest s)))))))] | |
(mapi start coll)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment