Define a integer variable and add to it

Hi,

I am running this query:

Let(
{
matches: Map(
Paginate(Match(Index(“matches_by_leagueid”), 11517)),
Lambda(“ref”, Get(Var(“ref”)))
),
wins: Map(
Var(“matches”),
Lambda(“match”, If(Select([“data”, “radiant_win”], Var(“match”)),Add(Var(“radiant_wins”),1),Add(Var(“dire_wins”),1)))
),
radiant_wins: 0,
dire_wins: 0
},
Var(“wins”)
)

matches is a array containing true/false bool, so I want to go through matches evaluate if that match has radiant_win = true and add +1 to radiant_wins variable, add +1 to dire_wins if its false.

I am getting:
“code”: “invalid expression”,
“description”: “Variable ‘dire_wins’ is not defined.”

full error:

Summary

Error: [
{
“position”: [
“let”,
1,
“wins”,
“map”,
“expr”,
“then”,
“add”,
0
],
“code”: “invalid expression”,
“description”: “Variable ‘radiant_wins’ is not defined.”
},
{
“position”: [
“let”,
1,
“wins”,
“map”,
“expr”,
“else”,
“add”,
0
],
“code”: “invalid expression”,
“description”: “Variable ‘dire_wins’ is not defined.”
}
]

Please post your schema as well.

One suggestion to calculate radiant wins: use Filter to grab where radiant_win = true, then use Count to total up how many wins there were. And you can do a second operation with a different Filter where radiant_wins = false to get the number of dire_wins

If you want a more performant version, you could create an index where the terms are leagueid and radiant_win, and then Match on them to get the set of radiant_wins or dire_wins for a given leagueid in a single operation, and use Count on those sets.

The reason why your code probably isn’t working as posted above, is because you can’t mutate variables in Fauna. So if you wanted to “add up” some numbers, you should either get them into the form of an array and use functions like Count or Sum, or you can use a function like Reduce to keep Adding a new value to a running count, and return that new value until the end where the final Add value is returned from the operation.

1 Like

Hi,

Thank you, I thought so as well about not being able to mutate variables - your suggestions pointed me in the exact right direction so thank you!