6
$\begingroup$

Reading in the doc, it is mentioned in the example of Dynamic that this way of writing the code does not work:

data = {.1, .5, .3, .9, .2};
Table[Slider[Dynamic[data[[i]]]], {i, 5}]

The reason is because Dynamic is HoldFirst.

Now is that really the reason? look at this:

Clear[f]
SetAttributes[f, HoldFirst]
f[x_] := g[x]
Table[f[i], {i, 5}]
(*{g[1], g[2], g[3], g[4], g[5]}*)

It is clear that the substitution of i happed to the function f inside Table.

So what is the reason that substitution of i happed in the f case and does not happened in the Dynamic case.

$\endgroup$
9
$\begingroup$

This is more like Dynamic:

Clear[f]
SetAttributes[f, HoldFirst]
(*f[x_]:=g[x]*)
Table[f[i], {i, 5}]
(*  {f[i], f[i], f[i], f[i], f[i]}  *)

By giving f a definition, it evaluated while in Table, during which the i got evaluated.

$\endgroup$
  • $\begingroup$ Is it like that? so Dynamic is not really a function! $\endgroup$ – Algohi Oct 14 '15 at 18:11
  • 4
    $\begingroup$ @Algohi Dynamic hasn't any hidden properties. The difference is that in the first table and Michael's example the i stays as a held argument. Dynamic stays to inform FE to take care of it. In your second example HoldFirst is acting only for a moment and it's passed to g which doesn't have Hold* attributes. $\endgroup$ – Kuba Oct 14 '15 at 19:06
  • $\begingroup$ @Kuba, ok I got it. Thanks $\endgroup$ – Algohi Oct 14 '15 at 20:22
  • $\begingroup$ @Michael E2 one last thing to make it clear, if the case is as you said and as @kuba said, then why f and Dynamic behaves differently in example like this {Slider[Dynamic[x]], Dynamic[x + 1], f[x + 1]} $\endgroup$ – Algohi Oct 14 '15 at 20:33
  • 1
    $\begingroup$ @Algohi Both are sending held x+1 to Kernel, but in first case DynamicBox[x+1] is back while in second f[x+1] boxes. And boxes are static unless FrontEnd decides they are not, and it decides "better keep track of that DynamicBox content". Maybe I shouldn't say Dynamic has no hidden properties due to the way FrontEnd treats them. Dynamic related tutorials may be of help. I'm reading them from time to time and there is always something I missed previously. $\endgroup$ – Kuba Oct 14 '15 at 20:52
0
$\begingroup$

I think I have different vision about what the problem is.

It looks like the reason is not because the HoldFirst attribute of Dynamic but rather because of Dynamic and Slider themselves. Slider and Dynamic does not respond to Hold function and to HoldAll Attributes of Table

In all of these example, Hold does not work:

HoldComplete[Slider[]]
HoldComplete[Slider[Dynamic[x]]]
HoldComplete[Slider[Dynamic[x]]]
HoldComplete[Dynamic[x + 1]]

enter image description here

In the above mentioned case of Table, Table has attribute HoldAll. But this does not work with Slider and Dynamic.

a = {.1, .5, .3, .9, .2};
Table[Slider[Dynamic[data[[i]]]], {i, 2}] // Trace
Table[Dynamic[data[[i]]], {i, 2}] // Trace

enter image description here

As you can see that Table does not hold its argument.

It looks like the Dynamic HoldFirst is not the reason for this issue but it is the Slider and Dynamic themselves evaluate inside Hold.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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