How do I find the index of an item in a vector?

后端 未结 8 1912
孤独总比滥情好
孤独总比滥情好 2020-11-29 01:13

Any ideas what ???? should be? Is there a built in? What would be the best way to accomplish this task?

(def v [\"one\" \"two\" \"three\" \"two         


        
8条回答
  •  旧时难觅i
    2020-11-29 01:26

    (defn find-thing [needle haystack]
      (keep-indexed #(when (= %2 needle) %1) haystack))
    

    But I'd like to warn you against fiddling with indices: most often than not it's going to produce less idiomatic, awkward Clojure.

提交回复
热议问题