7

If I define some function like:

toISOdate[data_List]:=DateString[data,{"Year","-","Month","-","Day"}]

and try to applicate it in an Association

ass = <|"date" -> {{2014, 1, 1}, {2014, 1, 2}}|>

as:

Replace[ass, a:{_, _, _} :> toISOdate[a], ∞]

I get, the non calculated form:

<|"date" -> {toISOdate[{2014, 1, 1}], toISOdate[{2014, 1, 2}]}|>

instead of

<|"date" -> {"2014-01-01", "2014-01-02"}|>

If I use Normal into ass as:

Replace[Normal@ass, (a : {_, _, _}) :> toISOdate[a], ∞]

I get the correct calculation, but lose the association form.

Is this a bug? How can I get it evaluated with Association?

Using Mathematica V10.1 on Mac

PS: I can't loose the association structure, the real case is more complex.

Update:

ass /. a : {_, _, _} :> toISOdate[a]

Works as expected.

Murta
  • 26,275
  • 6
  • 76
  • 166
  • If you assign the result of the Replace function to a variable, and recall it, it's correct (v 10.2 Mac OS X). a = Replace[....]; a["date"] – kale Jul 29 '15 at 23:31
  • @Mr.Wizard I'm on Mac. I edited the question to add this information tks. – Murta Jul 29 '15 at 23:32

1 Answers1

5

Taliesin Beynon stated in reply to Held keys in associations:

Keys have to remain unevaluated for associations to have any efficiency advantages over ordinary lists of rules. There's no way around that.

Apparently this is simply another manifestation of Replacement inside held expression and the same solution applies:

Replace[ass, a : {_, _, _} :> RuleCondition @ toISOdate[a], -2]
<|"date" -> {"2014-01-01", "2014-01-02"}|>

The question then becomes why does ReplaceAll cause evaluation? I think it should not, and I think that is a bug just as it is for Map and AssociationMap.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371