Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

Consider this Dynamic list

Slider[Dynamic@i, {Range[2, 10]}]
MapThread[DirectedEdge, Gather[##] & /@ (Range[#] & /@ Dynamic@i)]

The error is

List expected at position 2 in MapThread[...]

. Although the output given by the function is correct:

MapThread[DirectedEdge, {{1}, {2}}]
(* {1 -> 2} *)

Edit:

The purpose of the list creating above is to create a list being exploited by Graph:

DynamicModule[{p = {{0, 0}, {2, 2}}, i = 2},    
 Grid[{
    {Dynamic[DirectedEdge @@ Range@i],
     Dynamic@N@Round[MousePosition["Graphics"], 0.5]},
    {(Dynamic@Thread[{{DirectedEdge @@ Range@i}, {p}}]), 
     EventHandler[Dynamic@Graph[{DirectedEdge @@ Range@i},
        VertexCoordinates -> p, PlotRange -> 5, GridLines -> Automatic,
        Frame -> True, 
        FrameTicks -> All], "MouseDown" :>
    {(AppendTo[p, N@Round[MousePosition["Graphics"], 0.5]];),++i}]}
      }, Frame -> All]]

enter image description here

With at the top the construction of the DirectedEdge and the location of the mouse, at the bottom my attempt to see what should the list look like in order to be replaced in

Graph[{#1},VertexCoordinates -> #2]

Once again I assume that it's a Dynamic issue.

share|improve this question
2  
You say: this simple code here. No. This code is not simple. If it was simple, you would have seen the problem allready ;). In cases like these, I take break the code apart, try each piece on its own to make sure it works ok, then build things back piece by piece. And try to spread things out a little. This always works. –  Nasser Aug 21 '13 at 12:47
    
I second what @Nasser says but in this case you also need to realize that Dynamic doesn't evaluate. It has the HoldFirst attribute and is only evaluated in the front end; while it looks in the front end like you're passing a list you're really passing Dynamic[list] to MapThread and that's a no go. –  Pickett Aug 21 '13 at 12:58
    
That's what I thought too, but then how to pass the List instead of the Dynamic[List]? –  Öskå Aug 21 '13 at 13:10
    
Take Dynamic outside MapThread. Or use the sam trick which you unconsciously used with Range :) –  Kuba Aug 21 '13 at 13:11
    
Thanks all of you! :D Got it now! Should I delete the question? –  Öskå Aug 21 '13 at 13:19
show 5 more comments

marked as duplicate by Mr.Wizard Sep 2 '13 at 7:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 3 down vote accepted

"[...] generally the price of dynamic updating is change of Head." - Vitaliy Kaurov

so

Range[5]
Range[Dynamic@5]
{1, 2, 3, 4, 5}
Range::range: Range specification in Range[5] does not have appropriate bounds. >>
Range[5]

but, intentonaly or not, you've passed with Rangeand Gather because of Map :) and the error was generated by MapThread not earlier.

For the record, maybe it is due to the context and you have to use MapThread but in this particular case the following code is shorter:

Dynamic[DirectedEdge @@ Range@i]

If you need to use MapThread and create lists in form {{1},... then you may use Array:

Array[{#} &, 5]
{{1}, {2}, {3}, {4}, {5}}

You can stick with getting into Dynamic Head with Map if you want, just be consistent to the end: ;)

Slider[Dynamic@i, {Range[2, 10]}]

MapThread[DirectedEdge, #] & /@ (Array[{#} &, #] & /@ Dynamic@i)

Edit

Issue from the edit is because of inappropriate syntax, you can not specify Graph with sequence of numbers longer then two in DirectedEdges, they should be coupled:

Graph[ DirectedEdge @@@ Partition[Range@i, 2, 1] ...
share|improve this answer
    
DirectedEdge @@ Range@i is indeed shorter, thank you! –  Öskå Aug 21 '13 at 13:40
    
@Öskå I'm glad it helped, this was interesting because I've checked couple of things I wasn't sure :) –  Kuba Aug 21 '13 at 13:57
    
Although I'm having an other problem now, dealing with Dynamic again (on the same matter). Sould I edit the question or ask a new one? (I assume it has to do with the same trick, but can't figure out). –  Öskå Aug 21 '13 at 14:12
    
@Öskå and an edit to your question if it is closely related, I will edit my answer if I know the solution :) –  Kuba Aug 21 '13 at 14:59
    
Edited, hope that it's clear enough. –  Öskå Aug 21 '13 at 15:17
show 2 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.