Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

clojure

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"])  (defn find-thing [ thing vectr ]   (????))  (find-thing "two" v) ; ? maybe 1, maybe '(1,3), actually probably a lazy-seq 
like image 518
John Lawrence Aspden Avatar asked Jan 28 '11 16:01

John Lawrence Aspden


People also ask

What is the index of a vector?

Vector Indexing, or vector index notation, specifies elements within a vector. Indexing is useful when a MATLAB program only needs one element of a series of values. Indexing is often used in combination with repetition structures to conduct the same process for every element in an array.

Do vectors have indexes?

Indexing works on Vectors, So just Acces it by using index.

How do you return the index of an element in a vector C++?

int index = 0; for (auto const& element : myVec){ if(distance(element->position(), p) < d){ i = index; // the index of the current element } index++ ... Show activity on this post. &element - &myVec[0] should do the trick for container with continuous data (as std::vector ).


1 Answers

Built-in:

user> (def v ["one" "two" "three" "two"]) #'user/v user> (.indexOf v "two") 1 user> (.indexOf v "foo") -1 

If you want a lazy seq of the indices for all matches:

user> (map-indexed vector v) ([0 "one"] [1 "two"] [2 "three"] [3 "two"]) user> (filter #(= "two" (second %)) *1) ([1 "two"] [3 "two"]) user> (map first *1) (1 3) user> (map first             (filter #(= (second %) "two")                    (map-indexed vector v))) (1 3) 
like image 134
Brian Carper Avatar answered Sep 22 '22 21:09

Brian Carper