Hi, A quick thank you to both Gary and Sean and everyone! I will come back to this once I have achieved a deeper understanding.
Apologies for the delayed response. -- Kind regards Stephen. On 14 November 2017 at 09:59, Gary Verhaegen <gary.verhae...@gmail.com> wrote: > If you're going to call that for multiple elements of the same vector, > it's worth thinking about doing some work upfront so that each look-up is > faster: > > (defn indices [vect] > (->> vect > (map-indexed vector) > (reduce (fn [acc [idx el]] > (update acc el (fnil conj []) idx)) > {}))) > (indices [1 "is" 1 :same]){1 [0 2], "is" [1], :same [3]} > > > would walk through the vector only once and create a map of value -> list > of indices. > > On 14 November 2017 at 05:49, Sean Corfield <s...@corfield.org> wrote: > >> I don’t think anyone addressed your question about finding all the >> indices in a vector where the element matches a given search value? >> >> >> >> There are a number of ways to tackle that (given it’s going to be a >> linear search). Since you want the indices, you are either going to need to >> track them directly or use map-indexed to produce them automatically. >> >> >> >> (defn indices [x l] ; produces a vector >> >> (loop [i 0 l l r []] >> >> (if (seq l) >> >> (recur (inc i) (rest l) (if (= x (first l)) (conj r i) r)) >> >> r))) >> >> >> >> (defn indices [x l] ; produces a lazy sequence >> >> (keep identity (map-indexed (fn [i v] (when (= x v) i)) l))) >> >> >> >> (defn indices [x l] ; produces a vector >> >> (transduce (comp (map-indexed vector) >> >> (filter (comp (partial = x) second)) >> >> (map first)) >> >> conj >> >> [] >> >> l)) >> >> >> >> Hopefully that’ll give you some options to think about… >> >> >> >> Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN >> An Architect's View -- http://corfield.org/ >> >> "If you're not annoying somebody, you're not really alive." >> -- Margaret Atwood >> >> >> ------------------------------ >> *From:* clojure@googlegroups.com <clojure@googlegroups.com> on behalf of >> Stephen Feyrer <stephen.fey...@gmail.com> >> *Sent:* Monday, November 13, 2017 5:19:32 PM >> *To:* clojure@googlegroups.com >> *Subject:* Re: Map Keywords are functions, why not vector elements? >> >> Hi Alex, Didier, >> >> Thanks for your patience. >> >> That covers everything which I can think of and a fair bit more :) I >> have a bit of reading and thinking to do now. >> >> Again, thank you. >> >> >> -- >> Rule of thumb simple question, complicated answer >> >> Stephen. >> >> On 13 November 2017 at 22:09, Didier <didi...@gmail.com> wrote: >> >>> Yo are looking for indexOf (.indexOf vector value). >>> >>> (.indexOf ["a" "b"] "b") >>> => 1 >>> >>> (.indexOf ["a" "b" "b"] "b") >>> => 1 >>> >>> Note how indexOf searches for the index of the first value which matches >>> value. >>> >>> To do what you ask, is a query over a vector, which requires a search on >>> each element. This will take O(N) time. For a small list like in your >>> example its probably good enough and not an issue. >>> >>> If you want the name of the horse in a given position, that's a key >>> lookup, which is ~O(1) time. You can use get: >>> >>> (get ["a" "b"] 1) >>> => "b" >>> >>> If you really needed performance, you would need to combine a LinkedList >>> and a map. Some datastructures do it for you under the hood, like Apache >>> LinkedMap, amalloy ordered, java LinkedHashMap, etc. >>> >>> Its possible to also just use sorted-map-by in a closure. But this only >>> works if you're not going to add/update things to the datastructure after >>> first creation. >>> >>> See the example on clojuredocs: https://clojuredocs.org/clojur >>> e.core/sorted-map-by#example-542692d5c026201cdc327094 >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Clojure" group. >>> To post to this group, send email to clojure@googlegroups.com >>> Note that posts from new members are moderated - please be patient with >>> your first post. >>> To unsubscribe from this group, send email to >>> clojure+unsubscr...@googlegroups.com >>> For more options, visit this group at >>> http://groups.google.com/group/clojure?hl=en >>> --- >>> You received this message because you are subscribed to the Google >>> Groups "Clojure" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to clojure+unsubscr...@googlegroups.com. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@googlegroups.com >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/d/optout. >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@googlegroups.com >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/d/optout. >> > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.