File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed
Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -323,7 +323,7 @@ does what?
323323* TODO make-array
324324* TODO make-hierarchy
325325* DONE map
326- * TODO map-indexed
326+ * DONE map-indexed
327327* DONE map?
328328* DONE mapcat
329329* DONE max
Original file line number Diff line number Diff line change @@ -1425,6 +1425,19 @@ reduces them without incurring seq initialization"
14251425 ([a b c] (f (if (nil? a) x a) (if (nil? b) y b) (if (nil? c) z c)))
14261426 ([a b c & ds] (apply f (if (nil? a) x a) (if (nil? b) y b) (if (nil? c) z c) ds)))))
14271427
1428+ (defn map-indexed
1429+ " Returns a lazy sequence consisting of the result of applying f to 0
1430+ and the first item of coll, followed by applying f to 1 and the second
1431+ item in coll, etc, until coll is exhausted. Thus function f should
1432+ accept 2 arguments, index and item."
1433+ [f coll]
1434+ (let [mapi (fn mpi [idx coll]
1435+ (lazy-seq
1436+ (when-let [s (seq coll)]
1437+ (cons (f idx (first s))
1438+ (mpi (inc idx) (rest s))))))]
1439+ (mapi 0 coll)))
1440+
14281441(defn keep
14291442 " Returns a lazy sequence of the non-nil results of (f item). Note,
14301443 this means false return values will be included. f must be free of
@@ -3363,6 +3376,10 @@ reduces them without incurring seq initialization"
33633376 ; ; keep-indexed
33643377 (assert (= [1 3 5 7 9 ] (keep-indexed #(if (odd? %1 ) %2 ) [0 1 2 3 4 5 6 7 8 9 10 ])))
33653378 (assert (= [2 4 5 ] (keep-indexed #(if (pos? %2 ) %1 ) [-9 0 29 -7 45 3 -8 ])))
3379+
3380+ ; ; map-indexed
3381+ (assert (= ([0 :a ] [1 :b ] [2 :c ]) (map-indexed #(vector % %2 ) [:a :b :c ])))
3382+
33663383
33673384 :ok
33683385 )
You can’t perform that action at this time.
0 commit comments