Skip to main content
deleted 6 characters in body
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751

Maybe not at the first sight but it is expected once you think more about this.

 assoc[[{"a", "b"}]] +=1

is

assoc[[{"a", "b"}]] = (assoc[[{"a", "b"}]] + 1 )

and while rhs operation is still ok, the following one is not defined for associations (not counting generic assignment to values):

assoc[[{"a", "b"}]] = <|"a" -> 2, "b" ->3|>

in case of Lists only dimensions play a role for threading but here key names alsokeys too. There are Merge and friends for this kind of operations and by default it will just treat it as a single element rhs case, assigning association to each value.

Trace also makes sense:

assoc[[{"a", "b"}]] += 1; // Trace
{ assoc[[{a,b}]]+=1;
, { assoc[[{a,b}]]+=1
, { { assoc
    , <|a->1,b->2|>
    }
  , <|a->1,b->2|>[[{a,b}]]
  , <|a->1,b->2|>
  }
, { assoc[[{a,b}]]=<|a->2,b->3|>
  , <|a->2,b->3|>
  }
, <|a->2,b->3|>
}
, Null
}

Maybe not at the first sight but it is expected once you think more about this.

 assoc[[{"a", "b"}]] +=1

is

assoc[[{"a", "b"}]] = (assoc[[{"a", "b"}]] + 1 )

and while rhs operation is still ok, the following one is not defined for associations (not counting generic assignment to values):

assoc[[{"a", "b"}]] = <|"a" -> 2, "b" ->3|>

in case of Lists only dimensions play a role for threading but here key names also. There are Merge and friends for this kind of operations and by default it will just treat it as a single element rhs case, assigning association to each value.

Trace also makes sense:

assoc[[{"a", "b"}]] += 1; // Trace
{ assoc[[{a,b}]]+=1;
, { assoc[[{a,b}]]+=1
, { { assoc
    , <|a->1,b->2|>
    }
  , <|a->1,b->2|>[[{a,b}]]
  , <|a->1,b->2|>
  }
, { assoc[[{a,b}]]=<|a->2,b->3|>
  , <|a->2,b->3|>
  }
, <|a->2,b->3|>
}
, Null
}

Maybe not at the first sight but it is expected once you think more about this.

 assoc[[{"a", "b"}]] +=1

is

assoc[[{"a", "b"}]] = (assoc[[{"a", "b"}]] + 1 )

and while rhs operation is still ok, the following one is not defined for associations (not counting generic assignment to values):

assoc[[{"a", "b"}]] = <|"a" -> 2, "b" ->3|>

in case of Lists only dimensions play a role for threading but here keys too. There are Merge and friends for this kind of operations and by default it will just treat it as a single element rhs case, assigning association to each value.

Trace also makes sense:

assoc[[{"a", "b"}]] += 1; // Trace
{ assoc[[{a,b}]]+=1;
, { assoc[[{a,b}]]+=1
, { { assoc
    , <|a->1,b->2|>
    }
  , <|a->1,b->2|>[[{a,b}]]
  , <|a->1,b->2|>
  }
, { assoc[[{a,b}]]=<|a->2,b->3|>
  , <|a->2,b->3|>
  }
, <|a->2,b->3|>
}
, Null
}
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751

Maybe not at the first sight but it is expected once you think more about this.

 assoc[[{"a", "b"}]] +=1

is

assoc[[{"a", "b"}]] = (assoc[[{"a", "b"}]] + 1 )

and while rhs operation is still ok, the following one is not defined for associations (not counting generic assignment to values):

assoc[[{"a", "b"}]] = <|"a" -> 2, "b" ->3|>

in case of Lists only dimensions play a role for threading but here key names also. There are Merge and friends for this kind of operations and by default it will just treat it as a single element rhs case, assigning association to each value.

Trace also makes sense:

assoc[[{"a", "b"}]] += 1; // Trace
{ assoc[[{a,b}]]+=1;
, { assoc[[{a,b}]]+=1
, { { assoc
    , <|a->1,b->2|>
    }
  , <|a->1,b->2|>[[{a,b}]]
  , <|a->1,b->2|>
  }
, { assoc[[{a,b}]]=<|a->2,b->3|>
  , <|a->2,b->3|>
  }
, <|a->2,b->3|>
}
, Null
}