> ## Documentation Index
> Fetch the complete documentation index at: https://gql.ch/llms.txt
> Use this file to discover all available pages before exploring further.

# REDUCE

REDUCE function applies a lambda value expression to each element in a list, perform cumulative calculations, and return a single value.

### Syntax

```gql theme={null}
reduce(accumulator = initial_value, variable IN list | expression)
```

### Aggregate weights along PATH(s) in GQL

Suppose we are interested in the Uroosa's aggregated percentage in Hotel Kitchen Sink

```mermaid theme={null}
%%{init: {'theme':'base', 'themeVariables': {'lineColor':'#F1FA8C','secondaryColor':'#F1FA8C','background':'#282A36'}}}%%
graph TB
    fatima[:person <br/>Fatima 👩]
    uroosa[:person <br/>Uroosa 👩]
    zainab[:person <br/>Zainab 👩]
    hannah[:person <br/>Hannah 👩]
    maryam[:person <br/>Maryam 👩]

    acmecorp[:business <br/>Acme Corp. 🏢]
    northwind[:business <br/>Northwind 🏢]
    fourthcoffee[:business <br/>Fourth Coffee ☕🏢]
    tasmaniantraders[:business <br/>Tasmanian Traders 🏢]
    coffeecorp[:business <br/>Coffee Corp. ☕🏢]
    blueyonder[:business <br/>Blue Yonder 🏢]
    hotelkitchensink[:business <br/>Hotel Kitchen Sink 🏨]


    uroosa -->|:owns <br/>0.80| acmecorp
    fatima -->|:owns <br/>0.20| acmecorp
    uroosa -->|:owns <br/>0.15| hotelkitchensink
    hannah -->|:owns <br/>0.25| hotelkitchensink

    northwind -->|:owns <br/>0.09| hotelkitchensink
    acmecorp -->|:owns <br/>0.30| fourthcoffee
    fourthcoffee -->|:owns <br/>0.51| hotelkitchensink
    zainab -->|:owns <br/>0.19| fourthcoffee
    maryam -->|:owns <br/>1.00| coffeecorp
    coffeecorp -->|:owns <br/>0.51| blueyonder
    blueyonder -->|:owns <br/>0.51| fourthcoffee
    tasmaniantraders -->|:owns <br/>0.49| blueyonder

    
    classDef peoplenodes fill:#BD93F9,stroke-width:0px
    class eva,fatima,angela,uroosa,eleni,scott,zainab,hannah,maryam peoplenodes
    class northwind,acmecorp businessnodes
	

    classDef subgraphStyle fill:#282A36, stroke-width:0px
    class row1,row2,row3 subgraphStyle
```

We can use REDUCE as following:

```gql theme={null}
match path = all (a:person {_id:'uroosa'})-[owns]->{0,} (b:business {_id:'hotel kitchen sink'})
return path, 
reduce(total_ownership = 1 , own in owns | total_ownership * own.percentage) as ownership
next 
return sum(ownership);
```

Note that we first mutiply Ownership percentages along each path and then the sum up the percentages for the 2 paths.

### Calculating Geometric Mean using REDUCE

Geometric Mean is a measure of central tendency that calculates an average of the data using multiplication instead of addition. For a set of n numbers, the geometric mean is the nth root of the product of those numbers. For example, the geometric mean of the numbers 2, 3, and 14 equals (2 \* 3 \* 14)1/3 = (84)1/3 = 4.37952. Geometric Mean is particularly useful when the data is skewed and a simple average does not work.

Suppose we are interested in the Geometric Mean Income of all the people who live in the same city as Fatima:

```mermaid theme={null}

%%{init: {'theme':'base', 'themeVariables': {'lineColor':'#F1FA8C','secondaryColor':'#F1FA8C','background':'#282A36'}}}%%
graph LR

    subgraph row1[" "]

    saqib[<b>:person</b><br/>name: Saqib👨<br/>income: 375000]
    scott[<b>:person</b><br/>name: Scott 👨<br/>income: 205000]
    fatima[<b>:person</b><br/>name: Fatima 👩<br/>income: 195000]
    hannah[<b>:person</b><br/>name: Hannah 👩<br/>income: 185000]
    zainab[<b>:person</b><br/>name: Zainab 👩<br/>income: 190000]
    capitola[<b>:city</b><br/>name:Capitola]

    end

    subgraph row2[" "]
    eleni[<b>:person</b><br/>name: Eleni 👩<br/>income: 185000]
    uroosa[<b>:person</b><br/>name: Uroosa 👩<br/>income: 175000]
    angela[<b>:person</b><br/>name: Angela 👩<br/>income: 195000]
    maryam[<b>:person</b><br/>name: Maryam 👩<br/>income: 155000]
    santacruz[<b>:city</b><br/>name:Santa Cruz]
    end

    subgraph row3[" "]
    laila[<b>:person</b><br/>name: Laila 👩<br/>income: 180000]
    huda[<b>:person</b><br/>name: Huda 👩<br/>income: 172000]
    end

    %% saqib -->|:relationship<br/>type: co-worker| fatima
    %% fatima -->|:relationship<br/>type: friend| eleni
    %% eleni -->|:relationship<br/>type: friend| uroosa
    %% saqib -->|:relationship<br/>type: friend| angela
    %% angela -->|:relationship<br/>type: co-worker| scott
    %% scott -->|:relationship<br/>type: friend| uroosa


    saqib -->|<b>:lives_in</b>| capitola
    scott -->|<b>:lives_in</b>| capitola
    fatima -->|<b>:lives_in</b>| capitola
    hannah -->|<b>:lives_in</b>| capitola
    zainab -->|<b>:lives_in</b>| capitola



    angela -->|<b>:lives_in</b>| santacruz
    uroosa -->|<b>:lives_in</b>| santacruz
    eleni -->|<b>:lives_in</b>| santacruz
    maryam -->|<b>:lives_in</b>| santacruz


    classDef nodes fill:#BD93F9,stroke-width:0px
    class saqib,fatima,angela,uroosa,eleni,scott,maryam,zainab,hannah,huda,laila nodes

    classDef subgraphStyle fill:#282A36, stroke-width:0px
    class row1,row2,row3 subgraphStyle
```

```gql theme={null}
match path = ALL  (a {_id:'fatima'})-[:lives_in]->(b)<-[:lives_in]-(c)
return COLLECT_LIST(c) as residents
next
return reduce (geo_mean_product = 1, resident in residents | resident.income * geo_mean_product)  ^ (1.0 / LENGTH(residents)) as income_geometric_mean;
```

Note that we first use the [COLLECT\_LIST](/docs/functions/collect-list) to get all the people who live in the same city as Fatima in a LIST. Next we use the [REDUCE](/docs/functions/reduce) to calculate the Geometric Mean.

An alternate way to calculate Geometric Mean would be:

```gql theme={null}
match path = ALL  (a {_id:'fatima'})-[:lives_in]->(b)<-[:lives_in]-(c)
return exp(AVG(ln(c.income))) as income_geometric_mean;
```
