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} *)


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

DynamicModule[{p = {{0, 0}, {2, 2}}, i = 2},    
    {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.

  • 2
    $\begingroup$ 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. $\endgroup$ – Nasser Aug 21 '13 at 12:47
  • $\begingroup$ 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. $\endgroup$ – C. E. Aug 21 '13 at 12:58
  • $\begingroup$ That's what I thought too, but then how to pass the List instead of the Dynamic[List]? $\endgroup$ – Öskå Aug 21 '13 at 13:10
  • $\begingroup$ Take Dynamic outside MapThread. Or use the sam trick which you unconsciously used with Range :) $\endgroup$ – Kuba Aug 21 '13 at 13:11
  • $\begingroup$ Thanks all of you! :D Got it now! Should I delete the question? $\endgroup$ – Öskå Aug 21 '13 at 13:19

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


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

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)


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] ...
  • $\begingroup$ DirectedEdge @@ Range@i is indeed shorter, thank you! $\endgroup$ – Öskå Aug 21 '13 at 13:40
  • $\begingroup$ @Öskå I'm glad it helped, this was interesting because I've checked couple of things I wasn't sure :) $\endgroup$ – Kuba Aug 21 '13 at 13:57
  • $\begingroup$ 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). $\endgroup$ – Öskå Aug 21 '13 at 14:12
  • $\begingroup$ @Öskå and an edit to your question if it is closely related, I will edit my answer if I know the solution :) $\endgroup$ – Kuba Aug 21 '13 at 14:59
  • $\begingroup$ Edited, hope that it's clear enough. $\endgroup$ – Öskå Aug 21 '13 at 15:17

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