Indexing

Syntax

	$list.indexBy($key-fun)

$list.indexBy($key-fun, $clash-fun)

Description

Assigns a key value to each value in the list.

Unlike 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 clash-fun.

Parameter

Name Type Description Mandatory Default
list Set A list to be indexed. yes
key-fun Function Key function according to which the indexes are to be displayed. yes
clash-fun Function Clash function after which key duplicates can be resolved. Without this function duplicates will generate error messages. no No resolution of duplicates.

Return value

Type: Business object

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

Examples

Indexing by name shortcut

	list("Smith Joe", "Parker Jane", "Taylor Tom", "Black Tyler", "Brown Erik")
   .indexBy(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"
}

From each name, the first 3 letters are used as an index.

Indexing by name shortcut with clash function

	list("Smith Joe", "Parker Jane", "Brown Erik", "Taylor Tom", "Black Tyler", "Brook Ashley", "Browning Dave")
   .indexBy(name -> $name.subString(0, 3), 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" get the same keys by the key function. Without clash function an error message would come out. In this case the names for the key are put together.