Indexing

Syntax

	$list.indexBy($keyFun, $clashFun, $mapFun)

Description

Assigns a key value to each value in the list.

In contrast to grouping, the values in the resulting dictionary are not lists, but individual elements. Therefore, the calculated key values must be unique for the elements of the input set. Key conflicts can be resolved by specifying an optional clashFun. You can also specify mapFun to determine which value is saved for each key in the resulting dictionary.

Parameters

Key name Type Type Description Mandatory Default
list Quantity A list that is to be indexed. yes
keyFun Function Key function according to which the indices are to be generated. yes
clashFun function Clash function according to which key duplicates can be resolved. Without this function, duplicates generate error messages. If a mapFun is also specified, the mapped values are obtained, not the original entries from the list. no No resolution of duplicates.
mapFun function Map function that transforms the values stored in the list. Takes a parameter (the list element) and returns the value to be saved. The transformation takes place after the key calculation has been completed. no Original values are saved.

Return value

Type: Specialized object

A dictionary in which all values in the list are assigned to an index.

Examples

Indexing by name abbreviation

	list("Smith Joe", "Parker Jane", "Taylor Tom", "Black Tyler", "Brown Erik")
   .indexBy(keyFun: name -> $name.subString(0, 3))

Output: A dictionary with the values:

{
   "Smi":  "Smith Joe",
   "Par":  "Parker Jane",
   "Tay":  "Taylor Tom", 
   "Bla":  "Black Tyler",
   "Bro":  "Brown Erik"
}

The first 3 letters of each name are used as an index.

Indexing by name abbreviation with clash function

	list("Smith Joe", "Parker Jane", "Brown Erik", "Taylor Tom", "Black Tyler", "Brook Ashley", "Browning Dave")
   .indexBy(keyFun: name -> $name.subString(0, 3), clashFun: name1 -> name2 -> $name1 + " and " + $name2)

Output: A dictionary with the values:

{
   "Smi":  "Smith Joe",
   "Par":  "Parker Jane",
   "Tay":  "Taylor Tom", 
   "Bla":  "Black Tyler",
   "Bro":  "Brown Erik and Brook Ashley and Browning Dave"
}

The three names "Brown Erik", "Brook Ashley", "Browning Dave" are given the same keys by the key function. Without the clash function, an error message would be displayed. In this case, the names are combined for the key.

Indexing by name abbreviation with clash function and map function

[{"name": "Smith", "age": 45},
 {"name": "Parker", "age": 32},
 {"name": "Brown", "age": 28},
 {"name": "Brown", "age": 35},
 {"name": "Brown", "age": 33},
 {"name": "Taylor", "age": 41}
].indexBy(
    keyFun: person -> $person["name"],
    clashFun: age1 -> age2 -> ($age1 + $age2) / 2,
    mapFun: person -> $person["age"]
)

Output: A dictionary with the values:

{Smith=45.0, Parker=32.0, Brown=32.25, Taylor=41.0}

The names are used as keys. The age is extracted from each person object and saved as a value (mapFun). If several persons have the same name, the ages are averaged in pairs (clashFun).