[Cs254f11] Basic vector question

Omri Bernstein ob08 at hampshire.edu
Sun Oct 9 19:56:55 EDT 2011


I think I get your meaning, you have a vector with a vector nested in
it--like [1 2 [1 5] 3 4]--right? If you want to get a value within a vector
within a vector you can use the function "get-in". For example:

(def example-vec
  [1 2 [1 5] 3 4])

(get-in example-vec [2 0])
>> 1

(get-in example-vec [2 1])
>> 5

The "get-in" function takes a collection and then a vector of what it should
use on (what is equivalent to) recursive calls to "get". So the first
example above is the same thing as doing (get 0 (get 2 example-vec)). If you
had a nested vector within your nested vector, you could access those values
directly by doing something like this:

(def example-vec2
  [1 2 [1 [10 12] 5] 3 4])

(get-in example-vec [2 1 0])
>> 10

(get-in example-vec [2 1 1])
>> 12

Hope it helps.

-Omri


On Sun, Oct 9, 2011 at 6:41 PM, James Matheson <jmatheson12 at gmail.com>wrote:

> This is a little basic but how do you access an element within a
> vector? I know (nth x) or (get x) will return the value at that index,
> but if I'm trying to get something where the value is [1, 5] how do I
> access 1 or 5 directly?
>
> Thanks in advance,
> Jamie
> _______________________________________________
> Cs254f11 mailing list
> Cs254f11 at lists.hampshire.edu
> https://lists.hampshire.edu/mailman/listinfo/cs254f11
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.hampshire.edu/pipermail/cs254f11/attachments/20111009/53ee2b80/attachment.htm>


More information about the Cs254f11 mailing list