Skip to content

Association ​

A keyed collection of rules key -> value.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageAssociation✓
Association(k1 -> v1, …)a key -> value map, built from RuleRule pairs.
Typed signatures
TypePackageApplies
(rules: expression<Rule>*) -> anycombinatoricsalways

Domain: Collections

Details
  • Its own operator, not compute-engine's DictionaryDictionary: DictionaryDictionary's keys are strings only, and every example here keys on a plain number.
  • Length, First, Last, Join and Sort are extended to recognize an AssociationAssociation and answer in terms of its values, falling through to their ordinary list handling otherwise.
  • Not a general replacement for DictionaryDictionary or a full port of Wolfram's Association: only the operations the examples below exercise are implemented.

Examples

Basic #

Length counts the key-value pairs

live ↗
InLength(Association(Rule(1,2),Rule(3,4)))\mathrm{Length}(\mathrm{Association}(\mathrm{Rule}(1, 2), \mathrm{Rule}(3, 4)))Out22

First is the first value, not the first rule

live ↗
InFirst(Association(Rule(1,a),Rule(2,b)))\mathrm{First}(\mathrm{Association}(\mathrm{Rule}(1, a), \mathrm{Rule}(2, b)))Outaa

Last is the last value

live ↗
InLast(Association(Rule(1,a),Rule(2,b)))\mathrm{Last}(\mathrm{Association}(\mathrm{Rule}(1, a), \mathrm{Rule}(2, b)))Outbb
Scope #

Join merges keys; a later value for a repeated key wins, in the key's original position

live ↗
InJoin(Association(Rule(a,b)),Association(Rule(c,d),Rule(a,f)))\mathrm{Join}(\mathrm{Association}(\mathrm{Rule}(a, b)), \mathrm{Association}(\mathrm{Rule}(c, d), \mathrm{Rule}(a, f)))OutAssociation(Rule(a,f),Rule(c,d))\mathrm{Association}(\mathrm{Rule}(a, f), \mathrm{Rule}(c, d))

Sort orders the entries by value

live ↗
InSort(Association(Rule(a,4),Rule(b,1),Rule(c,3)))\mathrm{Sort}(\mathrm{Association}(\mathrm{Rule}(a, 4), \mathrm{Rule}(b, 1), \mathrm{Rule}(c, 3)))OutAssociation(Rule(b,1),Rule(c,3),Rule(a,4))\mathrm{Association}(\mathrm{Rule}(b, 1), \mathrm{Rule}(c, 3), \mathrm{Rule}(a, 4))
About
Examples5
Checked againstWolfram Language 5 agree
CrosswalkWolfram Language

See also: Length, First, Last, Join, Sort