Skip to content

Latest commit

 

History

History
53 lines (45 loc) · 1.33 KB

collect_map.md

File metadata and controls

53 lines (45 loc) · 1.33 KB

Aggregate Function

collect_map — aggregate map values into a single map

Synopsis

collect_map(|{any:any}|) -> |{any:any}|

Description

The collect_map aggregate function combines map inputs into a single map output. If collect_map receives multiple values for the same key, the last value received is retained. If the input keys or values vary in type, the return type will be a map of union of those types.

Examples

Combine a sequence of records into a map:

echo '{stock:"APPL",price:145.03} {stock:"GOOG",price:87.07}' |
  super -z -c 'collect_map(|{stock:price}|)' -

=>

|{"APPL":145.03,"GOOG":87.07}|

Continuous collection over a simple sequence:

echo '|{"APPL":145.03}| |{"GOOG":87.07}| |{"APPL":150.13}|' |
  super -z -c 'yield collect_map(this)' -

=>

|{"APPL":145.03}|
|{"APPL":145.03,"GOOG":87.07}|
|{"APPL":150.13,"GOOG":87.07}|

Create maps by key:

echo '{stock:"APPL",price:145.03,day:0}
      {stock:"GOOG",price:87.07,day:0}
      {stock:"APPL",price:150.13,day:1}
      {stock:"GOOG",price:89.15,day:1}' |
  super -z -c 'collect_map(|{stock:price}|) by day |> sort' -

=>

{day:0,collect_map:|{"APPL":145.03,"GOOG":87.07}|}
{day:1,collect_map:|{"APPL":150.13,"GOOG":89.15}|}