overpy icon indicating copy to clipboard operation
overpy copied to clipboard

Incorrect code generation for mapped and filtered arrays

Open Iluvatar opened this issue 2 years ago • 1 comments

When using list comprehensions that are both mapping and filtering, the generated code first does one then the other, leading to inconsistencies.

Example:

A = [2, 2, 4, 4]
T = [i for e, i in A if e == 4]

You'd expect T to be [2, 3]. However, it compiles to

Set Global Variable(T, Mapped Array(Filtered Array(Global.A, Compare(Current Array Element, ==, 4)), Current Array Index));

which first filters, then maps, leading to [2, 2, 4, 4] => [4, 4] => T = [0, 1].

It seems the workshop doesn't allow mapping and filtering at the same time. A rather heavy handed solution might be to first map elements to [elem, index], then filter, then map back out. Or just include a warning or something.

Iluvatar avatar Aug 25 '22 17:08 Iluvatar

Indeed, that's a corner case that I didn't realize occurred when they added the current array index. I think I'll add a warning, though I've pretty much stopped working on overpy until I see what they're doing with the workshop in ow2. Thanks for reporting :)

Zezombye avatar Aug 26 '22 07:08 Zezombye