Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given the following MWE, where:

lst = {<|"a" -> 1, "b" -> 2|>,
   <|"a" -> 2, "b" -> 2|>,
   <|"a" -> 3, "b" -> 2|>,
   <|"a" -> 4, "b" -> 2|>,
   <|"a" -> 5, "b" -> 2|>};

and the problem is that:

if the value of a is greater than 3 then "c"-> True must be added to the association.

I have the following =working= code

Map[If[#[[1]] > 3, var = #; AssociateTo[var, "c" -> True], #] &, lst]

which yields:

{<|"a" -> 1, "b" -> 2|>, 
<|"a" -> 2, "b" -> 2|>, 
<|"a" -> 3, "b" -> 2|>, 
<|"a" -> 4, "b" -> 2, "c" -> True|>, 
<|"a" -> 5, "b" -> 2, "c" -> True|>}

I prefer If-less programming and the var=# looks so unnecessary.

Is there a more "elegant" solution to this problem?

share|improve this question
    
It would appear that you can add keys with at least Join, Append, Prepend, <|..., key -> val|>. I guess this was your real question. – Szabolcs 1 hour ago
    
Related: (51472) – Mr.Wizard 1 hour ago

# and If -less approach:

lst /. a : KeyValuePattern["a" -> n_ /; n > 3] :> <|a, "c" -> True|>
share|improve this answer
    
So there is an If-less version after all. Beautiful code. – nilo de roock 1 hour ago
1  
@niloderoock Is If-less a primary metric of success here? – Mr.Wizard 1 hour ago
    
I have this hypothesis that IF's are red flags for poor program design: "When the IF's come in, the quality of software starts to deteriorate." – nilo de roock 50 mins ago
    
In refactoring code ( i.e. Java ) I aim to remove all IF's which always leads to cleaner design and code with much less LOC. – nilo de roock 27 mins ago

I recommend

If[#a > 3, Append[#, "c" -> True], #] & /@ lst

if you can tolerate a bit of If.

share|improve this answer
    
Yes, me too, too much If though. – Kuba 1 hour ago
    
@Kuba Well, you gave the Ifless version and there were a few small things to improve in the original that may make the If bearable after all. Consider it an alternative. – Szabolcs 1 hour ago
1  
Right. p.s. don't know if better, but shorter: <|#, "c" -> True|> – Kuba 1 hour ago
    
@Kuba Thank you, I did not know this. Do you know why Association is not Flat? – Szabolcs 1 hour ago
1  
Sorry, I don't. I'd ask WReach if I'd like to know ;) – Kuba 1 hour ago

Terse style:

<|#, If[#a > 3, "c" -> True, {}]|> & /@ lst
{
 <|"a" -> 1, "b" -> 2|>,
 <|"a" -> 2, "b" -> 2|>,
 <|"a" -> 3, "b" -> 2|>,
 <|"a" -> 4, "b" -> 2, "c" -> True|>,
 <|"a" -> 5, "b" -> 2, "c" -> True|>
}

For what it's worth version 10.1 does not have KeyValuePattern, but this works:

lst /. x_ /; x["a"] > 3 :> <|x, "c" -> True|>

And just to play with other methods:

<|#, DeleteCases[<|"c" -> #a > 3|>, False]|> & /@ lst
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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