0
$\begingroup$

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.

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

1 Answer 1

3
$\begingroup$

"[...] 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] ...
$\endgroup$
7
  • $\begingroup$ DirectedEdge @@ Range@i is indeed shorter, thank you! $\endgroup$
    – Öskå
    Commented Aug 21, 2013 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
    Commented Aug 21, 2013 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å
    Commented Aug 21, 2013 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
    Commented Aug 21, 2013 at 14:59
  • $\begingroup$ Edited, hope that it's clear enough. $\endgroup$
    – Öskå
    Commented Aug 21, 2013 at 15:17

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