[BUG] The where lambda in (folded:) applies to the starting value even when that value is explicitly supplied
The documentation for (folded:)
describes the usage of a where clause in the lambda with an example like the following:
(set: _numbers to (a: 1, 2, 3))
(folded: _item making _total via _total + _item where _item > 0, 0, ..._numbers)
This works as expected.
However, when the data type of the initial value and the rest of the items do not match — e.g. when building a string from a datamap — the where clause incorrectly also applies to the initial value, making the macro impossible.
e.g.
(set: _meals to (array:
(dm: "meal", "Breakfast", "yummy", true),
(dm: "meal", "Toast", "yummy", false),
(dm: "meal", "Dinner", "yummy", true)
))
(folded: _meal making _mealList via _mealList + ", " + _meal's meal where _meal's yummy is true, "", ..._meals)
This gives the error:
You can only access position strings/numbers ('4th', 'last', '2ndlast', (2), etc.), slices ('1stTo2ndlast', '3rdTo5th'), 'length', 'some', 'any', 'all', 'start', 'end', and 'random' of an empty string, not "yummy".
This appears to be because the "where" clause of the lambda is also applied to the initial empty string value, not just to each of the elements passed through the making ... via
. Conversely it is not possible to remove that default string, because then the first element passed to the lambda is the entire datamap and not a string element of it (i.e. _mealList
starts as (dm: "meal", "Breakfast", "yummy", true)
).
From the way the documentation describes the where clause, I think this is a bug when an explicit starting value is supplied, as the where should only filter elements drawn from the rest of the list