7
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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"] $\endgroup$
    – kale
    Commented Jul 29, 2015 at 23:31
  • $\begingroup$ @Mr.Wizard I'm on Mac. I edited the question to add this information tks. $\endgroup$
    – Murta
    Commented Jul 29, 2015 at 23:32

1 Answer 1

5
$\begingroup$

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.

$\endgroup$
12
  • $\begingroup$ RuleCondition is undocumented no? +1 $\endgroup$
    – Murta
    Commented Jul 30, 2015 at 0:28
  • $\begingroup$ @Murta Yes indeed, but very convenient. WReach introduced it to public use (at least within Stack Exchange) here, I believe. $\endgroup$
    – Mr.Wizard
    Commented Jul 30, 2015 at 1:47
  • $\begingroup$ Very interesting to know. $\endgroup$
    – Murta
    Commented Jul 30, 2015 at 1:51
  • 1
    $\begingroup$ @Mr.Wizard This feels like a bug to me... consider reporting it to Tech Support and I'll see what various other people in the company think. $\endgroup$ Commented Jul 31, 2015 at 4:32
  • 2
    $\begingroup$ @Mr.Wizard I don't like this behavior and I've advocated against it within the company. Did you report it as a bug to Tech Support? $\endgroup$ Commented Mar 19, 2017 at 11:59

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.