-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
foreach loop #116
Comments
It is not even Fortran???
pon., 16 gru 2019, 13:58 użytkownik Libavius <notifications@github.com>
napisał:
… Allow for foreach loops:
eg.
for element in array:
write(*,*) element
instead of
for i in SIZE(array):
write(*,*) array(i)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#116?email_source=notifications&email_token=AC4NA3LCABQCS7HGQ4E4B33QY53PLA5CNFSM4J3JQTG2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IAXI7SA>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AC4NA3OFU6MV7ACITOFIL3TQY53PLANCNFSM4J3JQTGQ>
.
|
I was a bit distracted when I wrote it. However, I edited it after max. 5 mins, which is 2 hours before you commented =/. |
PSA: edits are not reflected in the email notifications. If someone is only viewing these via email, they never see any edits. :) |
That is an interesting idea (probably the final |
Jup, I deleted the I sometimes prefer the foreach loop, if I have some kind of nested object oriented code: ie: type :: Molecule_t
type(Atom_t), allocatable :: atoms(:)
end type Molecule_t And later: subroutine Foo(molecules)
type(Molecule_t), intent(inout) :: molecules(:)
integer :: i, j
do i=1, SIZE(molecules)
do j=1, SIZE(molecules(i)%atoms)
call molecules(i)%atoms(j)%Do_something()
end do
end do
end subroutine Foo Would reduce to (probably the variables subroutine Foo(molecules)
type(Molecule_t), intent(inout) :: molecules(:)
type(Molecule_t), pointer :: molecule
type(Atom_t), pointer :: atom
do molecule in molecules
do atom in molecule%atoms
call atom%Do_something()
end do
end do
end subroutine Foo |
@libavius thanks for the use case. |
It could analogoulsy work for multidimensional arrays: (Note that the slicing of an array is determined by the way it is written in the memory.) subroutine Foo(tensor)
real(real64), intent(inout) :: tensor(:,:,:)
real(real64), pointer :: matrix(:,:), vector(:), scalar
do matrix in tensor
do vector in matrix
do scalar in vector
call Do_something(scalar)
end do
end do
end do
end subroutine Foo However, this is not really advantageous compared to the obvious way to do it. |
This could also enable some pythonic 'list' (here: array) comprehension: type(Atom_t) :: atoms(n)
integer :: charges(n)
charges = [(atom%Get_charge(), atom in atoms)] instead of: integer :: i
type(Atom_t) :: atoms(n)
integer :: charges(n)
charges = [(atoms(i)%Get_charge(), i=1, n)] EDIT: it is actually easier, if type(Atom_t) :: atoms(n)
integer :: charges(n)
charges = atoms%Get_charge() |
Also, how about adding Python iterator like capability to a derived type? That would be very interesting. |
There is the proposal on coroutines and iterators - [https://j3-fortran.org/doc/year/19/19-169.pdf] (see #60). That would definitely cover the kind of loop that is being discussed here. |
Allow for foreach loops:
eg.
instead of
The text was updated successfully, but these errors were encountered: