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.
Answer
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
.
Comments
Post a Comment