Evaluate[] seems to not work inside Button[]
Asked Answered
T

3

7

Any idea how to get this to work?

y = {}; Table[Button[x, AppendTo[y, Evaluate[x]]], {x, 5}]

Result: Click [1] , click [2], get {6,6}

I'm trivializing the actual task, but the goal is to set what a button does inside a Map or a Table or ParallelTable.

Please Help!


EDIT
Figured it out... Evaluate works at first level only. Here, it's too deep. So I used ReplaceRule:

Remove[sub]; y = {}; Table[Button[x, AppendTo[y, sub]] /. sub -> x, {x, 5}]
Tapestry answered 23/9, 2011 at 3:7 Comment(1)
Please post Remove[sub]; y = {}; Table[ Button[x, AppendTo[y, sub]] /. sub -> x, {x, 5}] as an answerSubtile
C
5

Replacement rules and pure functions offer concise alternatives to With. For example:

y={}; Range[5] /. x_Integer :> Button[x, AppendTo[y, x]]

or

y = {}; Replace[Range[5], x_ :> Button[x, AppendTo[y, x]], {1}]

or

y = {}; Array[Button[#, AppendTo[y, #]] &, {5}]

or

y = {}; Button[#, AppendTo[y, #]] & /@ Range[5]

For another example comparing these techniques, see my post here, where they are applied to a problem of creating a list of pure functions with parameter embedded in their body (closures).

Commendation answered 23/9, 2011 at 22:56 Comment(0)
F
10

This is a job for With. With is used to insert an evaluated expression into another expression at any depth -- even into parts of the expression that are not evaluated right away like the second argument to Button:

y = {}; Table[With[{x = i}, Button[x, AppendTo[y, x]]], {i, 5}]

In simple cases like this, some people (myself included) prefer to use the same symbol (x in this case) for both the With and Table variables, thus:

y = {}; Table[With[{x = x}, Button[x, AppendTo[y, x]]], {x, 5}]
Flowage answered 23/9, 2011 at 4:21 Comment(2)
+1. It is tangential to the current discussion, but at the bottom of my post in this thread: groups.google.com/group/comp.soft-sys.math.mathematica/…, I defined a macro, which, when wrapped around With[...], forces it to inject unevaluated expression(s) into its body. I mention it here because I thought you might find it interesting.Commendation
+1 For explaining the scoping construct in your ow words (and not WRI ones, which are not clear enough)Subtile
C
5

Replacement rules and pure functions offer concise alternatives to With. For example:

y={}; Range[5] /. x_Integer :> Button[x, AppendTo[y, x]]

or

y = {}; Replace[Range[5], x_ :> Button[x, AppendTo[y, x]], {1}]

or

y = {}; Array[Button[#, AppendTo[y, #]] &, {5}]

or

y = {}; Button[#, AppendTo[y, #]] & /@ Range[5]

For another example comparing these techniques, see my post here, where they are applied to a problem of creating a list of pure functions with parameter embedded in their body (closures).

Commendation answered 23/9, 2011 at 22:56 Comment(0)
T
0

Evaluate works at first level only. Here, it's too deep. So I used ReplaceRule:

Remove[sub]; y = {}; Table[ Button[x, AppendTo[y, sub]] /. sub -> x, {x, 5}] 
Tapestry answered 27/9, 2011 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.