diff --git a/README.md b/README.md index aa4c3b0..5d4f57d 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ smart people at [Hashrocket](http://hashrocket.com/). - [Evaluate One Liners With lein-exec](clojure/evaluate-one-liners-with-lein-exec.md) - [Expanding Macros](clojure/expanding-macros.md) - [Get The Value Of An Environment Variable](clojure/get-the-value-of-an-environment-variable.md) +- [Mapping With An Index](clojure/mapping-with-an-index.md) - [Open JavaDocs](clojure/open-javadocs.md) - [Pretty Print The Last Thing](clojure/pretty-print-the-last-thing.md) - [Quick Clojure Docs](clojure/quick-clojure-docs.md) diff --git a/clojure/mapping-with-an-index.md b/clojure/mapping-with-an-index.md new file mode 100644 index 0000000..a1caa93 --- /dev/null +++ b/clojure/mapping-with-an-index.md @@ -0,0 +1,11 @@ +# Mapping With An Index + +If you're mapping over a collection and you need an index for each item, you +can reach for `map-indexed`. The `map-indexed` function can be used like so: + +```clojure +> (def foods ["pizza" "hotdog" "chicken alfredo"]) +#'cljs.user/foods +> (map-indexed (fn [idx item] (str idx " - " item)) foods) +("0 - pizza" "1 - hotdog" "2 - chicken alfredo") +```