[Cs254f11] 'Un-nesting' sequences to be used in assoc

Omri Bernstein ob08 at hampshire.edu
Tue Dec 6 20:38:42 EST 2011


Max,

Two thoughts. One, if you did (apply assoc board (apply concat
*vector-sequence-here*)), that would work. So, using that example list you
gave, and blank hash-map {} for "board":
(apply concat [[[0 0] 1] [[0 1] 1] [[1 2] 1] [[0 3] 1]])
>> ([0 0] 1 [0 1] 1 [1 2] 1 [0 3] 1)
(apply assoc {} (apply concat [[[0 0] 1] [[0 1] 1] [[1 2] 1] [[0 3] 1]]))
>> {[0 0] 1, [0 1] 1, [1 2] 1, [0 3] 1}

You could also use reduce here. This would instead be like recursively
calling assoc on each key value pair, then assoc on the return value of the
previous assoc and the next key value pair, etc, until the last key value
pair. As in, (reduce (fn [m [k v]] (assoc m k v)) board
*vector-sequence-here*). With the example sequence and {} for board:
(reduce (fn [m [k v]] (assoc m k v)) {}  [[[0 0] 1] [[0 1] 1] [[1 2] 1] [[0
3] 1]])
>> {[0 0] 1, [0 1] 1, [1 2] 1, [0 3] 1}

Best,
-Omri


On Tue, Dec 6, 2011 at 7:59 PM, Maxwell William Fair Levit <
mwl10 at hampshire.edu> wrote:

> Hi, I'm trying to get a sequence of vectors of vectors that looks like
> this:
>
> ([[0 0] 1] [[0 1] 1] [[1 2] 1] [[0 3] 1])
>
> to be the key value pairs of a call to assoc
>
> The map I'm associng into has key values that are two place vectors, so if
> I
> were to manualy input a mapping it would look like
>
> (assoc board [0 0] 1 [2 2] 5) and so on.
>
> Is there some way to un-sequence my list? Keep in mind that this is a
> specific
> arbitrary example, and my  sequence of vectors will vary in length
> throughout a
> single execution.
>
> -Max
>
> _______________________________________________
> 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/20111206/ce864453/attachment.htm>


More information about the Cs254f11 mailing list