Syntax Proposals / Foreach loop
Is your feature request related to a problem? Please describe.
When importing arrays, we need to know while writing the model the size of the imported array, as it is needed to generate the i
of the for loop. Something this can be quite inconvenient.
Describe the solution you'd like We could extend the loop mechanism to authorize "foreach" behaviour:
#PARAMETERS
x = import "long_file_with_unknown_length.csv";
//...
#CONSTRAINTS
a == sum(v for v in x)
Semantically, we have multiple option:
- We can, as above, say that when we do
for v in x
,v
we take each value contained inx
. This is the behavior in most languages. It's quite natural for arrays (#9). - ... but it doesn't work for dicts (#10).
- In that case we may want to iterate on keys of the dict, but it would introduce a small discrepancy between arrays and dicts.
- We can also forbid to iterate directly on dicts, and introduce functions such as
keys(x)
orvalues(x)
that returns an array of the keys/values ofx
. They would also work to get the keys of an array. - Or we can say that a foreach loop always iterate on the keys, even for arrays:
#PARAMETERS x = import "long_file_with_unknown_length.csv"; //... #CONSTRAINTS a == sum(x[i] for i in x)
I believe the most "natural" way of handling things is either "value on arrays/keys on dicts" or "always value on arrays, forbid dict iteration, and keys(x)/values(x) to work on dicts".
Alternatives This can be done, for arrays only, by creating a function that returns the size of an array (#15).