# Tag Info

55

Generally, you want the Trott-Strzebonski in-place evaluation technique: f[x_Real]:=x^2; Hold[{Hold[2.],Hold[3.]}]/.n_Real:>With[{eval = f[n]},eval/;True] (* Hold[{Hold[4.],Hold[9.]}] *) It will inject the evaluated r.h.s. into an arbitrarily deep location in the held expression, where the expression was found that matched the rule pattern. This is ...

48

RuleCondition provides an undocumented, but very convenient, way to make replacements in held expressions. For example, if we want to square the odd integers in a held list: Hold[{1, 2, 3, 4, 5}] /. n_Integer :> RuleCondition[n^2, OddQ[n]] (* Hold[{1, 2, 9, 4, 25}] *) RuleCondition differs from Condition in that the replacement expression is evaluated ...

38

Preamble This is a very good question, because answering it will make it very clear what immutability means, both in general and in the context of Associations. General A few general words on immutability Associations are immutable data structures. This means that they carry no state, and a copy of an Association is another completely independent ...

24

Here are a couple of alternatives to Trott-Strzebonski in @R.M's answer: Hold[{3,4,5|6}] /. Verbatim[Alternatives][x__] :> RuleCondition@RandomChoice@List@x Hold[{3, 4, 5}] Hold[{3,4,5|6}] /. Verbatim[Alternatives][x__] :> Block[{}, RandomChoice@List@x /; True] Hold[{3, 4, 6}] They operate on the same principle as Trott-Strzebonski (...

23

Although less magical, it can be done by ReplacePart expr = Hold[{2, 3, 4, 5}] pos = Position[expr, _Integer] newparts = Extract[expr, pos] /. n_Integer :> n^2 ReplacePart[expr, Thread[pos -> newparts]]

22

This is a case where the Trott-Strzebonski in-place evaluation trick is useful. You use With to inject inside your held expression as: (Hold[{3, 4, 5 | 6}] /. (Verbatim@Alternatives)[x__] :> With[{eval = RandomChoice@List@x}, eval /; True]) Out[1]= Hold[{3, 4, 5}] You should definitely read this post by Leonid, that gives you a good insight into ...

22

I hesitate to add anything after @Leonid's comprehensive answer, but I'd like to point out that an easy way to achieve the stated goal is to define f like this: f[x_] := <| x, "isFirstValueTrue" -> x@"firstValue" |> ... which yields the desired result when mapped across the associations in x: f /@ x (* { <|"firstValue" -> True, "...

21

I think the problem might be related to a bug in FullForm when applied to a ByteArray object: ByteArray["aV+jpGtfd3BHhoSvOthJpQ=="] // FullForm (* List[105,95,163,164,107,95,119,112,71,134,132,175,58,216,73,165] *) The full form has lost information regarding the structure of the ByteArray. The box-form of the button is using this list form but the ...

18

It is because, in version 9, the implementation of Plot is loaded from a dump file on its first usage, rather than loading when the kernel starts. One can see this by clearing the ReadProtected attribute: ClearAttributes[Plot, ReadProtected] Information[Plot] (* -> Plot := SystemDumpAutoLoad[ Hold[Plot], Hold[syms], VisualizationProto ...

18

The way Mathematica works is that when it encounters a function with arguments it will try to evaluate the arguments first before proceeding to evaluate the function. This behavior can be modified by specifying the various HoldAll, HoldFirst, HoldRest, etc. attributes for a given function. So in your example f[x+1] will be immediately replaced by f[6] ...

17

Recall that the rendering of Graphics has nothing to do with evaluation. It is done entirely in typesetting. And therefore, a robust solution will treat this as a problem of typesetting, and not as a problem of evaluation. Once you frame the problem properly, the solution is fairly straightforward. What you want to do is to change the typesetting of Hold ...

16

How about this: list = {1, 2, 3}; ToExpression["list", InputForm, Hold] /. Hold[v_] :> AppendTo[v, 3] {1, 2, 3, 3} list {1, 2, 3, 3}

14

Not to detract from the existing answers (particularly @WReach's suggestion, which was the same solution that came to my mind as I read your question, and which I will use here), but you may find it easier to define your own references rather than using strings. (In fact, I wouldn't necessarily recommend an approach based on building Mathematica expressions ...

14

#1 Trott-Strzebonski in-place evaluation: hf = HoldForm[1 - 1^2/2 + 1^3/3 - 1^4/4 + 1^5/5 - 1^6/6] hf /. x_Times :> With[{eval = x}, eval /; True] 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 Replace[hf, x_ :> With[{eval = x}, eval /; True], {2}] 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 One may simplify this method using the undocumented function ...

13

The Hold functions enable Mathematica's version of what some other languages call "macros." You can use them for a lot of things, but the essential point is that they preserve the structure of the input. The built-in functions are full of examples: x = 7; Plot[x^2, {x, -2, 2}] Type this in and you'll see that Plot draws the parabola even though "x" was ...

13

You are missing Unevaluated: SetAttributes[f, HoldFirst] f[x_] := {SymbolName[Unevaluated@x], x} because SymbolName does not hold its arguments, so you have to prevent evaluation also there. Generally, if you are passing some argument via a chain of function calls, and want to keep it unevaluated, you have to prevent it's evaluation at each stage (...

13

While I can't follow your code, I guess your problem is caused by the fact that you get evaluation in between individual replacements, and the fact that Listable functions of several arguments (which includes operators like + and *) have quite peculiar behaviour. The fact that you get a matrix instead of a vector, as well as the fact that you can avoid it ...

13

I am betting that this is almost certainly an optimization short-cut. If Orderless had to try every ordering it would be extremely slow when there are a moderate number of arguments, but it is not. Consider for example: f @@@ Hold @@ {RandomSample[Range@12]} Hold @@ {f @@ Range@12 /. {7 -> _}} MatchQ[%%, %] Hold[f[2, 6, 11, 7, 12, 10, 4, 1, 3, 5, 8, ...

11

HoldForm[#1 + #2]&[RandomInteger[100], RandomInteger[100]] (* 77 + 84 *)

11

I propose: HoldForm[+##] & @@ RandomInteger[100, 2]

11

From prior comments I know that you are interested in forms such as: a - b - c - d a / b / c / d There is no simple short form for these as there is for Plus. To understand this you must understand how Mathematica parses and displays these expressions. Let's look at the first one: Subtract HoldForm[a - b - c - d] a - b - c - d No surprises. But ...

10

This is possible in the interactive session with \$PreRead. I will adopt my solution to the same problem posted in this Mathgroup thread. To quote my explanation from there, the essence of the present solution is to delay the parsing of the code (body) that must be executed inside a given context until run-time, that is, replace code BeginPackage[yourcontext]...

10

I think that in general, for tasks like this one, tricks like Trott-Strzebonski technique are not the best way, and one really needs expression parsers, which are may be not shorter, but more readable and more extensible. Here is a possible one for your problem: ClearAll[convert]; SetAttributes[convert, {HoldAll}]; convert[x_List] := Map[convert, ...

10

One way to achieve this is to use a "vanishing" wrapper. The idea is to temporarily wrap the substituted expression with a holding symbolic head, and then remove that head in a second replacement: Module[{h} , SetAttributes[h, HoldAll] ; y /. bar[j_] :> RuleCondition[Extract[x, {j}, h]] /. h[x_] :> x ] (* Hold[foo[2+2]] *) Module is used to ensure ...

10

General I think that one can achieve the goal much easier if we reformulate the request. A variable is IMO not a proper object to store an iterator in the form of expression. What you really need is an environment, which would use certain iterator in code. Simple lexical / dynamic environment Here is how it may look: ClearAll[withIterator]; ...

10

OK, now I have another suggestion: With[{x = ToString[encryptedObj, InputForm]}, Print[x]; Button["Try with", foo = Decrypt["pass", ToExpression[x]]]] The button generates no error and foo is set to "TestCase".

10

Reanalysis My earlier assertions were incorrect or at least incomplete. I now believe the problem in your code originates because of a particular behavior that can be seen in this separate example: asc = <|foo -> <|bar -> <|baz -> 1|>|>|> <|foo -> <|bar -> <|baz -> 1|>|>|> asc[foo][bar][baz] = 2; ...

10

Quick fix is to use e.g. Inactivate and friends: Manipulate[ Column[{ Activate @ #, Panel @ ExpressionCell[ # /. Inactive -> Defer, "Notebook", "Input" ] }] & @ Inactivate @ Plot[Sin[x], {x, -3, 3}, AxesLabel -> {xlabel, ylabel}, Ticks -> {tics, tics}] , Control[{{tics, Automatic, ""}, {Automatic -&...

10

ClearAll[a, b]; a := 1 + 1 b = Sqrt Is this acceptable? foo = # /. Join @@ Cases[#, s_Symbol :> OwnValues[s], ∞, Heads->True] &; foo @ Hold[a + b[c]] Hold[(1 + 1) + Sqrt[c]] Update As OP has noticed I've missed the fact that ReadProtected symbols won't show its OwnValues. We could do something like s_Symbol /; FreeQ[ Attributes[s], ...

9

You might want HoldForm[Plus[##]] & @@ (List @@ taylor /. x -> 1)

Only top voted, non community-wiki answers of a minimum length are eligible