During my time as a developer, both (and especially) in the academia and in production software, I've come across the problem of getting the index of those elements in an array that meet a constrain. This is easy (although after some training, as usual) in Clojure
(defn index-filter [pred coll]The way this works is: the for macro associates each i and elem to the pairs in the sequence given by (fn [a b] [a b]) coll). This is filtered by executing the predicate on each element. The predicate, in turn, filters out the elements in which we are not interested. The for body then returns each of the index that passed the condition.
(for [[i elem]
(map-indexed (fn [a b] [a b]) coll) :when (pred elem)]
i))
We can separate the functionality into two functions, the first to write the indexing of the original elements as an independent function:
(def make-index (partial map-indexed (fn [a b] [a b]) ))We use (partial) to make a function that still needs an input argument and associate it into the make-index symbol. Placing it into the general code:
(defn index-filter [pred coll]The way you call this function is with a predicate and a collection. For example, Now we have a very elegant solution that is valid for many data types.
(for [[i elem] (make-index coll) :when (pred elem)]
i))