There is a good chance that forA
is not the function you want to use here. That’s because the function you pass to it is only allowed to depend on the argument (quantity
), and the only form of “sequence”, or things happening in a particular order, comes from the actions you are performing with the do
. Specifically, variable binding is not such an action; it is purely a local alias for a value, and cannot escape its scope of definition. (To put it another way, DAML functions must be true functions, whose only “effect” is in accepting arguments and returning values.)
You might be comfortable with forA
from examples. However, it is not a “one size fits all” function for working across the aggregate of a list; it is used in those examples because it hides some details of lists that are sometimes useful, but not always. If there is any “one size fits all” function for lists, it’s foldr
or foldl
as @cocreature used previously, but you won’t see foldr
in most examples, because it is much nicer in DAML to use a function that more closely fits your use case, and foldr
is far too powerful and general-purpose for that.
To determine what the proper function to use instead of forA
is, first decide: are you performing any actions from the function you’re passing in, such as contract exercises or creates? If you are, a good starting point is the DA.Action
module; if you are not, check the DA.Foldable
module instead. Regardless, the starting point is to make that determination.