aradi / fypp

Python powered Fortran preprocessor
http://fypp.readthedocs.io
BSD 2-Clause "Simplified" License
180 stars 30 forks source link

Multiple tuple returns for #:for #12

Closed zerothi closed 3 years ago

zerothi commented 3 years ago

Great package ;)

Code explains all:

! This works great
#:set real_kinds = [('32', 'real32'), ('64', 'real64'), ('128', 'real128')]
#:for rname, rkind in real_kinds
...
#:endfor

! This does not work
#:for (lname, lkind), (hname, hkind) in zip(real_kinds[:-1], real_kinds[1:])
...
#:endfor

! This does not work
#:set low_high = list(zip(real_kinds[:-1], real_kinds[1:]))
#:for (lname, lkind), (hname, hkind) in low_high
...
#:endfor

! This WORKS:
#:set low_high = list(map(lambda a, b: a + b, real_kinds[:-1], real_kinds[1:]))
#:for lname, lkind, hname, hkind in low_high
...
#:endfor

It has to do with the parenthesis is some way.

aradi commented 3 years ago

Thanks a lot :smile: !

Currently, Fypp only allows only for very simple expressions as iteration variable: either a single variable name, or several variable names joined by comma. In order to ensure robustness, Fypp has to extract the individual variable names from the expression and make their assignments indidividually (instead of letting Python to handle the entire expression). Allowing for nested tuples in the iterator variable would make things orders of magnitude more complicated as they are now, and the benefit would be rather low in my opinion. Especially, as you demonstrate, there are not too complicated workarounds.

So, currently I don't see much chance to implement the nested tuples as loop variables in the near future (but code donation is always welcome :wink: ). However, I have updated the documentation (abd25189) to describe the loop variable syntax more in detail.