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

Lee Spector lspector at hampshire.edu
Tue Dec 6 20:47:06 EST 2011


Heh, yeah, Omri's first version is cleaner than all of mine, I think.

 -Lee


On Dec 6, 2011, at 8:38 PM, Omri Bernstein wrote:

> 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
> 
> _______________________________________________
> Cs254f11 mailing list
> Cs254f11 at lists.hampshire.edu
> https://lists.hampshire.edu/mailman/listinfo/cs254f11

--
Lee Spector, Professor of Computer Science
Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438



More information about the Cs254f11 mailing list