aradi / fypp

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

Append method for lists #9

Closed ivan-pi closed 3 years ago

ivan-pi commented 4 years ago

I was wondering how to use the .append() method of lists instead of the summation operator. The .pop() method works fine.

Minimal working example:

#:set a = [0]
#:set b = [1,2,3]
$:set a = a + [1]
#:set a = a + [2]
#:for i, ra in enumerate(a)
    print *, ${ra}$,${b.pop()}$
#:endfor
end

The output from fypp is:

    print *, 0,3
    print *, 1,2
    print *, 2,1
ivan-pi commented 4 years ago

I've realized now I can use an eval directive to perform the append operation:

#:set a = [0]
#:set b = [1,2,3]
$:a.append(1)
$:a.append(2)
#:for i, ra in enumerate(a)
    print *, ${ra}$,${b.pop()}$
#:endfor
end

However, this introduces two empty lines into the output:

$ fypp mwe.fypp 

    print *, 0,3
    print *, 1,2
    print *, 2,1

Is there some other way to use lists?

aradi commented 4 years ago

No, currently there is no other way IMO. I would recommend to use the

#:set a = a + [2]

form as it seems more readable to me. I don't think, that this causes serious speed bottleneck especially compared to the startup time of the Python interpreter.

Currently, Fypp only allows the evaluation of expressions. In theory, one could extend it with code blocks, e.g.

#:exec a.append(1)

While this would allow for greater flexibility, it would in my opinion dramatically increase the possibility of writing unreadable meta-code, instead of trying to implement most of it in pure Fortran.

But if you come up with a convincing show-case, I may rethink my point of view. :wink:

aradi commented 3 years ago

Closing this issue, as no show-case had been demonstrated, where the suggested way of doing the list extension in Fypp would be too slow. Feel free to re-open it, if necessary.