1
$\begingroup$

I want to construct an association that some of the Values are constructed from other parts of the Values, for the simplest example

assoc=<|
    "a"->1,
    "b"->assoc["a"]
|>;

The output I desired is

<|a->1,b->1|>

but to get this result we need to evaluate the Set twice

assoc=<|
    "a"->1,
    "b"->assoc["a"]
|>;

and the first evaluation returns

<|a->1,b->assoc[a]|>

I believe this is related to the inner construction of association, see https://mathematica.stackexchange.com/a/128456/86893.

Notice that this feature is different from the sequential CompoundExpression[a=1,b=a];

Is there some trick to bypass this problem?


The real example is much complicated than this example. What I want is a method similar to the "in-place evaluation trick". I have tried RuleCondition but failed for this example.

The method I currently use is like

assoc=<|
    "a"->1
|>
assoc=<|
    assoc,
    "b"->assoc["a"]
|>

As you can see, for more complicated situations this get harder to read and organize.

$\endgroup$

2 Answers 2

5
$\begingroup$

There is a function on the FunctionRepository that does what you want: AssociationTemplate

The basic use is:

tAssoc = ResourceFunction["AssociationTemplate"][
  <|
   "a" -> 3,
   "int" :> Function[NIntegrate[Abs@Sin[#a x], {x, 0, 10}]],
   "title" :> "The integral of |Sin[`a` x]| on 0 <= x <= 10 is `int`",
   "plot" :> Plot[Abs@Sin[TemplateSlot["a"] x], {x, 0, 10}, PlotLabel -> Style[TemplateSlot["title"], Small], Filling -> Axis]
   |>
  ]
$\endgroup$
1
  • $\begingroup$ Thanks, this template reminds me of the injection pattern trick. $\endgroup$
    – Lacia
    Commented Jan 12, 2023 at 18:49
5
$\begingroup$

@Juanito970's answer has reminded me of the injection pattern. To my understanding, the injection pattern is just like providing a template of more complicated code.

For the toy example, we can try

assoc=1/.x_:><| 
    "a"->x,
    "b"->x
|>
(*<|"a" -> 1, "b" -> 1|>*)
$\endgroup$
1
  • 1
    $\begingroup$ Very clever code, and yes it can be understood as a general code template $\endgroup$
    – JuanG970
    Commented Jan 12, 2023 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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