less than 1 minute read

Code

;; keep_indexed.clj
;; https://clojuredocs.org/clojure.core/keep-indexed

(def some-vector  [1 17 5 7 6 8 2 12 11 8])

(def number 8)

;; %1 means index
;; %2 means value in the collection
(keep-indexed #(when (odd? %1) %2) [:a :b :c :d :e])

(map-indexed #(when (odd? %1) %2) [:a :b :c :d :e])

(keep-indexed #(if (pos? %2) %1) [-9 0 29 -7 45 3 -8])

(map-indexed #(if (pos? %2) %1) [-9 0 29 -7 45 3 -8])

;; What they mean by pred here?
(defn indices [pred coll]
  (keep-indexed #(when (pred %2) %1) coll))

(indices #(= % number) some-vector)

;; % means %1, that is the index
(map-indexed #(when (< % 2) (str % %2)) [:a :b :c])

(keep-indexed #(when (< % 2) (str % %2)) [:a :b :c])

Updated: