5/5

a!update(): edit a cart line safely

Hard

A shop's cart stores the quantity on each line. When a shopper edits line ri!line, counting from 1:

  • a quantity of 0 removes that line
  • any other quantity replaces the old one
  • a line that does not exist — 0, negative, or past the end — leaves the cart unchanged

Left unguarded, a position past the end stretches the list and fills the gap with nulls.

Inputs

  • ri!quantities — List of Integer
  • ri!line — Integer
  • ri!quantity — Integer

ri! Rule Inputs

ri!quantitiesList of Integer
{
  2,
  1,
  5
}
ri!lineInteger
2
ri!quantityInteger
4

Example 1 — Change a quantity

Input: ri!quantities
{
  2,
  1,
  5
}
Input: ri!line
2
Input: ri!quantity
4
Output:
{
  2,
  4,
  5
}

Example 2 — Zero removes the line

Input: ri!quantities
{
  2,
  1,
  5
}
Input: ri!line
3
Input: ri!quantity
0
Output:
{
  2,
  1
}

Example 3 — A line past the end

Input: ri!quantities
{
  2,
  1,
  5
}
Input: ri!line
5
Input: ri!quantity
1
Output:
{
  2,
  1,
  5
}

Example 4 — Line zero

Input: ri!quantities
{
  2,
  1,
  5
}
Input: ri!line
0
Input: ri!quantity
3
Output:
{
  2,
  1,
  5
}

Example 5 — An empty cart

Input: ri!quantities
{}
Input: ri!line
1
Input: ri!quantity
2
Output:
{}

Appian Expression Tips

  • • Use ri!variableName for rule inputs
  • • Use a!localVariables(local!x: …, body) for locals
  • • Use a!forEach(items: …, expression: fv!item) to iterate
  • • Use where() / reject(fn!isnull, list) to filter